デバイスの向きを設定してUIViewを非同期に更新する問題に直面しています。以下のようにviewDidloadにデバイスの向きを実装しました
- (void)viewDidLoad{
[super viewDidLoad];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged) name:UIDeviceOrientationDidChangeNotification object:nil];
[self initialize];}
orientationChanged メソッドには、次のコードがあります
-(void)orientationChanged {
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if(UIInterfaceOrientationIsLandscape(orientation)){
UINib *nib = [UINib nibWithNibName:@"ConsoleViewControllerLandscape" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;
[self initialize];
} else {
UINib *nib = [UINib nibWithNibName:@"ConsoleViewController" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;
[self initialize];
}
初期化メソッドでは、実際に次のようなコードで UI を非同期に更新します
[self performSelectorOnMainThread:@selector(arrangeAsynchronously) withObject:nil waitUntilDone:NO];
- (void) arrangeAsynchronously{
//Some complex calculation and finally
[self.view addSubview:imageview];
}
問題は、向きが変更された imageViews がメイン ビューに追加されない場合です。縦向きビューから始めて、すべてのイメージビューを縦向きビューで見ることができ、横向きに変更するとビューが空白になるとしましょう。ポートレートに切り替えると、すべてのサブビュー、つまり imageViews が適切に追加されます。問題は、方向が変更されたときです。新しい nib ファイルをロードしていますが、コードはまだ古い nob ファイルからロードされた古いビューを参照しています。参照を変更するにはどうすればよいですか。この問題は、非同期モードで実行した場合にのみ発生します。
デバイスの回転後のサブビュー位置の計算ではなく、uiview の問題ではありません。以前の私のコードは
CGAffineTransform inverseTransform = CGAffineTransformInvert(self.view.transform);
fixedPoint = CGPointApplyAffineTransform(fixedPoint,inverseTransform);
fixedPoint = CGPointMake(fixedPoint.x+126, fixedPoint.y-109);
そして、私はそれをに変更しました
fixedPoint = CGPointMake(fixedPoint.x+126, fixedPoint.y-109);
しかし、なぜ affinetransform が waitUntilDone:NO で動作せず、waitUntilDone:YES で動作するのか、まだわかりません。