【问题标题】:How to get iPhones current orientation?如何获取 iPhone 的当前方向?
【发布时间】:2011-01-31 21:35:52
【问题描述】:

是否有特殊的方法来获取 iPhone 的方向?我不需要它的度数或弧度,我希望它返回一个 UIInterfaceOrientation 对象。我只需要它来进行像

这样的 if-else 结构
if(currentOrientation==UIInterfaceOrientationPortrait ||currentOrientation==UIInterfaceOrientationPortraitUpsideDown) {
//Code
}  
if (currentOrientation==UIInterfaceOrientationLandscapeRight ||currentOrientation==UIInterfaceOrientationLandscapeLeft ) {
//Code
}

提前致谢!

【问题讨论】:

    标签: objective-c accelerometer


    【解决方案1】:

    这很可能是您想要的:

    UIInterfaceOrientation interfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
    

    然后您可以使用系统宏,例如:

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation))
    {
    
    }
    

    如果你想要设备方向使用:

    UIDeviceOrientation deviceOrientation = [[UIDevice currentDevice] orientation];
    

    这包括像 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown 这样的枚举

    【讨论】:

    • 这在技术上是错误的,你从你的方法调用中获得了一个 UIDeviceOrientation 并将它存储在一个 UIInterfaceOrientation 中。 UIInterfaceOrientation 是 4 种之一,代表您的界面外观。 UIDeviceOrientation 是 6 种之一(包括平放和倒置),它告诉您设备的方向,而不一定是您的界面外观。您的界面可能处于纵向模式,但如果设备坐在桌子上,它将返回 UIDeviceOrientationFaceUp。
    • @CoryImdieke:感谢您更新设备和界面方向之间的差异。 UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation];是正确的 API
    • 这在技术上是错误的我同意@Cory,但效果很好。我将在下面发布一个不会出错并且是相同概念的方法。
    • 这适用于您不希望界面改变方向但同时又需要方向反馈而无需使用陀螺仪的情况。
    • statusBarOrientation 现已弃用。
    【解决方案2】:

    正如其他答案中所讨论的,您需要 interfaceOrientation,而不是 deviceOrientation。

    最简单的方法是使用 UIViewController 上的 interfaceOrientation 属性。 (所以最常见的是:self.interfaceOrientation 就可以了)。

    可能的值是:

    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft
    

    记住: 将您的设备向右转动即可输入左侧方向。

    【讨论】:

    • 可惜现在已弃用。
    • 是的,这已被弃用,我们现在该怎么办?
    【解决方案3】:

    这是我必须编写的一段代码,因为当方向改变时,我在根视图中遇到了一些奇怪的问题......确实似乎是....这很好用,没有错误

    - (void)viewDidAppear:(BOOL)animated
    {
        [super viewDidAppear:animated];
    
        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeLeft){
            //do something or rather
            [self 
    shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeLeft];
            NSLog(@"landscape left");
        }
        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationLandscapeRight){
            //do something or rather
            [self 
    shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationLandscapeRight];
            NSLog(@"landscape right");
        }
        if ([[UIDevice currentDevice]orientation] == UIInterfaceOrientationPortrait){
            //do something or rather
            [self shouldAutorotateToInterfaceOrientation:UIInterfaceOrientationPortrait];
            NSLog(@"portrait");
        }
    }
    

    【讨论】:

    • 这不适用于UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown
    【解决方案4】:

    UIInterfaceOrientation 现已弃用,UIDeviceOrientation 包括 UIDeviceOrientationFaceUpUIDeviceOrientationFaceDown,因此不能依赖它来为您提供界面方向。

    解决方案很简单

    if (CGRectGetWidth(self.view.bounds) > CGRectGetHeight(self.view.bounds)) {
        // Landscape
    } else {
        // Portrait
    }
    

    【讨论】:

      【解决方案5】:

      [UIApplication statusBarOrientation] 已被弃用,您现在应该使用:

      UIWindowScene *activeWindow = (UIWindowScene *)[[[UIApplication sharedApplication] windows] firstObject];
      
      UIInterfaceOrientation orientation = [activeWindow interfaceOrientation] ?: UIInterfaceOrientationPortrait;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多