【问题标题】:How do you tell which way up the iPhone is?你怎么知道 iPhone 是哪条路?
【发布时间】:2014-08-14 21:32:42
【问题描述】:

使用来自 iPhone 的 CMAttitude* 信息,您可以判断音高。 iPhone 是垂直的,它是 90º。 如果它是平坦的,它是 0º。 但是我有一个应用程序需要知道 iPhone 是朝上还是朝下/向前还是向后。 知道这是否可以做到吗?

>     * CMAttitude *attitude;
>       CMDeviceMotion *motion = motionManager.deviceMotion;
>       attitude = motion.attitude;

【问题讨论】:

    标签: ios cmmotionmanager


    【解决方案1】:

    您是否尝试过使用 UIDevice 类检查设备方向?

    已编辑: 首先添加一个观察者来监控方向变化:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientationDidChange:)
                                                     name:UIDeviceOrientationDidChangeNotification object:nil];
    }
    

    在回调中,获取当前设备方向

     - (void)orientationDidChange:(NSNotification*)notification
     {
         /// check current orientation
         switch ([[UIDevice currentDevice] orientation])
         {
             case UIDeviceOrientationPortrait:
                 break;
             case UIDeviceOrientationPortraitUpsideDown:
                 break;
             case UIDeviceOrientationLandscapeLeft:
                 break;
             case UIDeviceOrientationLandscapeRight:
                 break;
             case UIDeviceOrientationFaceUp:
                 break;
             case UIDeviceOrientationFaceDown:
                 break;
             default: /// UIDeviceOrientationUnknown
                 break;
         }
     }
    

    这将在设备改变方向时通知您。

    可在此处找到支持的方向: https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

    【讨论】:

    • 完美,这个调用会在有变化但不是当前状态时提醒您。我怀疑我可以假设 iPhone 将面朝上,然后如果它被移动可以纠正。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    相关资源
    最近更新 更多