UIDeviceOrientationFaceUp - 如何区分肖像和风景?

时间:2011-11-05 08:59:29

标签: iphone cocoa-touch ipad landscape-portrait uideviceorientation

我正在尝试查看设备是处于纵向还是横向模式。如果设备没有朝上,我的代码工作得很好。如果它面朝上(和方向== 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 个答案:

答案 0 :(得分:41)

你不应该混淆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]。提供here的替代方案是[[UIApplication sharedApplication] statusBarOrientation]。这也会返回UIInterfaceOrientation

答案 1 :(得分:5)

我制作这个代码骨架来处理想要的&amp;不需要的设备方向,在我的情况下,我想忽略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 */
        }
    }
}

答案 2 :(得分:0)

从iOS13开始使用

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