UIDeviceOrientationFaceUp - 如何区分竖屏和横屏?

21
我想知道设备是处于横屏还是竖屏模式。我的代码在设备没有朝上时很好用。但如果它朝上了(并且orientation == 5),它将无法区分横屏和竖屏。是否有办法在UIDeviceOrientation为FaceUp时确定“方向”(横屏/竖屏)呢?
我的代码:
UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

NSLog(@"orientation: %d", interfaceOrientation);

if (interfaceOrientation == UIDeviceOrientationIsLandscape(interfaceOrientation)) {
    NSLog(@"LANDSCAPE!!!");
}

if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) {
    NSLog(@"PORTRAIT!!!");
}
3个回答

43

你不应该混淆 UIDeviceOrientationUIInterfaceOrientation,它们是不同的但是相关的,正如它们的声明所示。

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

typedef enum {
   UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
   UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
   UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
   UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
UIDeviceOrientation表示设备的方向,UIInterfaceOrientation表示界面的方向并被UIViewController使用。 UIInterfaceOrientation肯定是竖屏或横屏,而UIDeviceOrientation可能具有模糊的值(UIDeviceOrientationFaceUpUIDeviceOrientationFaceDownUIDeviceOrientationUnknown)。

在任何情况下,不应使用[[UIDevice currentDevice] orientation]来确定UIViewController的方向,因为无论设备的方向如何,UIViewController interfaceOrientation属性都可能不同(例如,如果您的应用程序根本不旋转到横屏,则[[UIDevice currentDevice] orientation]可以为UIDeviceOrientationLandscapeLeft,而viewController.interfaceOrientation可以是UIInterfaceOrientationPortrait)。

更新:从iOS 8.0开始,[UIViewController interfaceOrientation]已被弃用。一个替代方案是这里提供的[[UIApplication sharedApplication] statusBarOrientation],它也返回UIInterfaceOrientation


5

我为处理所需和不需要的设备方向创建了此代码骨架,在我的情况下,我希望忽略UIDeviceOrientationUnknown、UIDeviceOrientationFaceUp和UIDeviceOrientationFaceDown,并缓存最后允许的方向。这段代码适用于 iPhone 和 iPad 设备,对你可能有用。

- (void)modXibFromRotation {

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    NSString *device = [[UIDevice currentDevice]localizedModel];
    UIInterfaceOrientation cachedOrientation = [self interfaceOrientation];

    if ([device isEqualToString:@"iPad"]) {

        if (orientation == UIDeviceOrientationUnknown || 
            orientation == UIDeviceOrientationFaceUp || 
            orientation == UIDeviceOrientationFaceDown) {

                orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

            /* Your code */
        }

        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

            /* Your code */     
        }
    }

    if ([device isEqualToString:@"iPhone"] || [device isEqualToString:@"iPod"]) {

        if (orientation == UIDeviceOrientationUnknown || 
        orientation == UIDeviceOrientationFaceUp || 
        orientation == UIDeviceOrientationFaceDown) {

            orientation = (UIDeviceOrientation)cachedOrientation;
        }

        if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight) {

             /* Your code */
        }

        if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) {

            /* Your code */
        }
    }
}

1
截至iOS 13使用:

UIInterfaceOrientation orientation;
if (@available(iOS 13.0, *)) {
    orientation = self.window.windowScene.interfaceOrientation;
} else {
    orientation = [[UIApplication sharedApplication] statusBarOrientation];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接