ユーザーがUITableViewCellのボタンをタップして関連する画像をズームするコアアニメーションを使用して、デバイス全体(UIWindow)をカバーするUIViewを使用して、画像のズームイン/アウト効果をサポートします。
ズームは問題なく実行されていますが、デバイスが横向きになっているにもかかわらず、サブビューがまだ縦向きモードになっている理由を理解できませんでした。以下の図:
ナビゲーション コントローラーはありますが、このビューは UIWindow に直接追加されています。
ユーザーがUITableViewCellのボタンをタップして関連する画像をズームするコアアニメーションを使用して、デバイス全体(UIWindow)をカバーするUIViewを使用して、画像のズームイン/アウト効果をサポートします。
ズームは問題なく実行されていますが、デバイスが横向きになっているにもかかわらず、サブビューがまだ縦向きモードになっている理由を理解できませんでした。以下の図:
ナビゲーション コントローラーはありますが、このビューは UIWindow に直接追加されています。
考えられる原因のいくつかについては、こちらをご覧ください:
Technical Q&A QA1688 - UIViewController がデバイスと共に回転しないのはなぜですか?
あなたの状況では、おそらくビューを別のサブビューとしてウィンドウに追加しているという事実です。最初のサブビューのみが回転イベントを取得します。できることは、最初のウィンドウ サブビューのサブビューとして追加することです。
UIWindow* window = [UIApplication sharedApplication].keyWindow;
if (!window)
window = [[UIApplication sharedApplication].windows objectAtIndex:0];
[[[window subviews] objectAtIndex:0] addSubview:myView];
iOS 6 以降、最上位のビュー コントローラー (UIApplication オブジェクトと並んで) のみが、デバイスの向きの変化に応じて回転するかどうかの決定に参加します。
https://developer.apple.com/library/content/qa/qa1688/_index.html
AGWindowViewという名前のポッドをオープンソース化しました。
回転やフレームの変更は自動的に処理されるため、心配する必要はありません。
SDK と iOS システム バージョンの任意の組み合わせをサポートします。関連するコードはここにあります: https://github.com/hfossli/AGWindowView/blob/master/Source/AGWindowView.m
keyWindow の最初のサブビューを取得するためのヘルパー プロパティとメソッドを持つ UIApplication のカテゴリを作成しました。とにかくオーバーレイしたいビューです。UIViewController によって管理されるビューをそのビューに追加すると、 shouldRotateToInterfaceOrientation: メソッドが呼び出されます。
UIApplication+WindowOverlay.h
#import <UIKit/UIKit.h>
@interface UIApplication(WindowOverlay)
@property (nonatomic, readonly) UIView *baseWindowView;
-(void)addWindowOverlay:(UIView *)view;
@end
UIApplication+WindowOverlay.m
#import "UIApplication+WindowOverlay.h"
@implementation UIApplication(WindowOverlay)
-(UIView *)baseWindowView{
if (self.keyWindow.subviews.count > 0){
return [self.keyWindow.subviews objectAtIndex:0];
}
return nil;
}
-(void)addWindowOverlay:(UIView *)view{
[self.baseWindowView addSubview:view];
}
@end
使用方法は次のとおりです。
//at the top of the file...or in {yourproject}.pch
#import "UIApplication+WindowOverlay.h
//in a method:
UIView *view = [UIView new];
UIView *window = [UIApplication sharedApplication].baseWindowView;
view.frame = window.bounds;
[window addSubview:view];
//or
[[UIApplication sharedApplication] addWindowOverlay:view];
これは、ビューが UIWindow に直接追加されたことに言及したためです。したがって、回転するメソッドがナビゲーション コントローラーに対して呼び出されても、UIView には何も起こりません。ビュー コントローラー ビューのサブビューである場合、UIView は回転します。何らかの理由でこれができない場合。次に、このメソッドをオーバーライドできます。
// This method is called every time the device changes orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
}
また、向きが変わるたびに、ビューの向きも変わります。
ビューがウィンドウに直接追加されるという同様の問題がありました。たぶんこれが役に立ちます:ウィンドウに追加した後に UIView を自動的にサイズ変更する
この問題を解決した別の解決策。
現在の方向を定義します。
@interface AJImageCollectionViewController (){
UIInterfaceOrientation _currentOrientation;
}
@end
次に、viewWillLayoutSubviews で向きを確認します。
- (void)viewWillLayoutSubviews {
[self checkIfOrientationChanged];
}
- (void)checkIfOrientationChanged {
UIInterfaceOrientation newOrientation = [[UIApplication sharedApplication] statusBarOrientation];
BOOL newOrientationIsPortrait = UIInterfaceOrientationIsPortrait(newOrientation);
BOOL oldOrientationIsPortrait = UIInterfaceOrientationIsPortrait(_currentOrientation);
// Check if the orientation is the same as the current
if(newOrientationIsPortrait != oldOrientationIsPortrait){
_currentOrientation = newOrientation;
// Do some stuff with the new orientation
}
}