ios - 当我 Handlebars 机放平时,UIDevice 方向不断在 UIDeviceOrientationPortrait 和 UIDeviceOrientationLandscapeRight 之间翻转

标签 ios iphone device-orientation

当我将手机平放时,我对设备方向值感到非常困惑。我为 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/

相关文章:

ios - Swift ios重置 View Controller 层次结构

iphone - 仅在 iPad 上从其他应用程序打开 URL,在 iPhone 上禁用

ios - 如果开关打开,如何强制所有屏幕的方向处于纵向模式?如果用户旋转它不应该改变它的方向

android - Google Play 游戏服务多人设备方向更改将用户踢出房间

ios - insertRowsAtIndexPaths : of custom UITableViewCells ends in unrecognized selector sent to instance

ios - 在 iOS 的 UI 自动化中,如何在执行 #import 之前等待 performTaskWithPathArgumentsTimeout 完成

ios - 是否可以将 UITests 目标中的文件复制到应用程序的文档目录中?

iOS 应用程序在设备上签名并安装后崩溃 : DYLD, 依赖 dylib '@rpath/LegacyDatabase.framework/LegacyDatabase' 未找到

ios - 如何在 Swift 的 For In 循环中打印自定义类?

ios - 在方向更改时更新以编程方式添加的 UIView 的框架/约束