我面临着在设备方向到位的情况下异步更新 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)都会正确添加。问题是当方向改变时,我正在加载一个新的 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);
但是我仍然不知道为什么仿射变换不起作用waitUntilDone:NO并且在waitUntilDone:YES中起作用。