我正在尝试检测任何设备方向变化,以便我可以更新 View 。
无论方向是纵向还是横向,我都想更新 View ,所以我实现了这个方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationLandscapeRight || interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
我知道如果我想更新 View 以正确显示当前方向,我将需要实现以下一些方法:
– willRotateToInterfaceOrientation:duration:
– willAnimateRotationToInterfaceOrientation:duration:
– didRotateFromInterfaceOrientation:
– willAnimateFirstHalfOfRotationToInterfaceOrientation:duration:
– didAnimateFirstHalfOfRotationToInterfaceOrientation:
– willAnimateSecondHalfOfRotationFromInterfaceOrientation:duration:
现在,问题是我不明白为什么当我旋转模拟器时这些方法都没有被触发。
我也试过这段代码:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
但还是没有。所以我想知道,模拟器会触发旋转通知吗? 如果是,我做错了什么?
最佳答案
你需要添加类似
的通知观察者[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
并添加方法
- (void) didRotate:(NSNotification *)notification
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(@"Landscape Left!");
}
}
关于iphone - 查看旋转通知 : why didRotateFromInterfaceOrientation: doesn't get called?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6111013/