当我将手机平放时,我对设备方向值感到非常困惑。我为 UIDeviceOrientationDidChangeNotification
注册了一个观察者,并调用 beginGenerateDeviceOrientationNotifications
来观察设备方向的变化。一切正常,直到我将手机平放在桌面上。
这是我的项目中的代码:
- (void)deviceDidRotate:(NSNotification *)notification
{
UIDevice *currentDevice = [UIDevice currentDevice];
NSLog(@"Orientation: %d", currentDevice.orientation);
}
日志给了我这个:
2013-12-29 12:14:28.121 MyProject[1140:60b] Orientation: 5
2013-12-29 12:14:28.764 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:31.609 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:33.377 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:33.574 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:33.770 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:33.868 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:34.065 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:34.164 MyProject[1140:60b] Orientation: 4
2013-12-29 12:14:34.455 MyProject[1140:60b] Orientation: 1
2013-12-29 12:14:34.555 MyProject[1140:60b] Orientation: 4
repeating...
前 5 个相当合理,因为我已将手机正面朝上,但 1s(代表 UIDeviceOrientationPortrait
)和 4s(代表 UIDeviceOrientationLandscapeRight
)相当令人困惑.
我的问题是如何解决这个问题?
最佳答案
我遇到了同样的问题,下面的解决方案解决了这个问题:
在每个 viewController 中添加以下方法(根据您的首选方向)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft);
}
- (BOOL)shouldAutorotate {
return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskLandscape;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationLandscapeLeft;
}
在 AppDelegate
中以及 didFinishLaunchingWithOptions
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(preferredInterfaceOrientationForPresentation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
关于ios - 当我 Handlebars 机放平时,UIDevice 方向不断在 UIDeviceOrientationPortrait 和 UIDeviceOrientationLandscapeRight 之间翻转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822114/