UIDeviceOrientationFaceUp - 如何区分纵向和横向?

Posted

技术标签:

【中文标题】UIDeviceOrientationFaceUp - 如何区分纵向和横向?【英文标题】:UIDeviceOrientationFaceUp - how to distinguish between portrait and landscape? 【发布时间】:2011-11-05 08:59:29 【问题描述】:

我正在尝试确定设备是处于纵向还是横向模式。如果设备没有朝上,我的代码运行良好。如果它确实面朝上(并且方向 == 5),它将无法区分纵向和横向。如果 UIDeviceOrientation 是 FaceUp,是否可以确定横向/纵向的“方向”?

我的代码:

UIDeviceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];

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

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


if (interfaceOrientation == UIDeviceOrientationIsPortrait(interfaceOrientation)) 
    NSLog(@"PORTRAIT!!!");

【问题讨论】:

【参考方案1】:

你不应该混淆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] 都可以是UIDeviceOrientationLandscapeLeftviewController.interfaceOrientation 可以是UIInterfaceOrientationPortrait)。

更新:ios 8.0 开始,[UIViewController interfaceOrientation] 已弃用。 here 提供的另一种选择是 [[UIApplication sharedApplication] statusBarOrientation]。这也会返回UIInterfaceOrientation

【讨论】:

【参考方案2】:

我制作这个代码框架来处理想要和不需要的设备方向,在我的例子中,我想忽略 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 */
        
    

【讨论】:

【参考方案3】:

从 iOS13 开始使用

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

【讨论】:

以上是关于UIDeviceOrientationFaceUp - 如何区分纵向和横向?的主要内容,如果未能解决你的问题,请参考以下文章