在MainViewController中,我必须调整一些图像的方向变化。简单--只需将代码添加到willAnimateRotationToInterfaceOrientation:回调
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
NSLog(@"orientation: %@", name(orientation));
..按预期工作。在画像中,我将另一个感知方向的UIViewController推到UINavigationController上。我转到风景,一切都是好的。
然后我通过调用MainViewController返回到
[self.navigationController popViewControllerAnimated:YES];当MainViewController的子视图根据自动调整大小的掩码调整为景观时,willAnimateRotationToInterfaceOrientation:不被调用!
我希望willAnimateRotationToInterfaceOrientation:不是在导航控制器堆栈的顶部时也会被调用,或者至少在返回到堆栈顶部时被调用。
我想我可以在流行之前手动调用willAnimateRotationToInterfaceOrientation:,但这感觉不对。
我遗漏了什么?
发布于 2010-08-29 23:10:58
在我看来,这是预期的行为。如果UIVC不在堆栈的顶部,那么就不应该调用willAnimateRotationToInterfaceOrientation,因为当时没有旋转。我在我现在使用的应用程序中处理这个问题的方式类似于上面的海报。任何支持所有方向的UIVC都会得到一个新的方法。
- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation;
这个方法从两个地方调用:
-(void) viewWillAppear: (BOOL) animated {
[super viewWillAppear: animated];
[self updateLayoutForNewOrientation: self.interfaceOrientation];
}
-(void) willAnimateRotationToInterfaceOrientation: (UIInterfaceOrientation) interfaceOrientation duration: (NSTimeInterval) duration {
[self updateLayoutForNewOrientation: interfaceOrientation];
}
The new method is simple:
- (void) updateLayoutForNewOrientation: (UIInterfaceOrientation) orientation {
if (UIInterfaceOrientationIsLandscape(orientation)) {
// Do some stuff
} else {
// Do some other stuff
}
}
Additionally, if you were worried about the code running when its not actually needed, you could track the orientation the device was in when the new UIVC was pushed on to the stack via an instance variable set in viewDidDisappear and consult it to decide if you want to "Do the stuff"发布于 2010-08-29 16:10:01
这不是正确的行为吗?因为动画回调只能在动画方向改变之前调用。当您弹出最上面的视图时,您将看到pop动画,然后在下面看到已经旋转的视图。没有旋转动画,因此没有回调。(但你肯定会收到willRotateToInterfaceOrientation:duration:。)
我有一个示例Xcode项目,用于在GitHub上进行界面定位实验,您可能会感兴趣。
发布于 2010-08-29 17:04:59
因为当您返回主视图时没有发生旋转,所以willAnimateRotationToInterfaceOrientation将不会被调用。但是,如果您希望根据更改的方向执行某些操作,则可以将以下代码添加到主视图控制器的viewWillAppear方法中
- (void)viewWillAppear:(BOOL)animated {
UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if(UIInterfaceOrientationIsPortrait(statusBarOrientation)){
NSLog(@"Portrait");
....your code....
}
else {
NSLog(@"Landscape");
....your code....
}
}https://stackoverflow.com/questions/3595471
复制相似问题