Ios 你如何判断iPhone的上升方向?

Ios 你如何判断iPhone的上升方向?,ios,cmmotionmanager,Ios,Cmmotionmanager,使用iPhone上的Cmatitude*信息,您可以判断音高。 如果iPhone是垂直的,则为90º。 如果是平的,则为0º。 但我有一个应用程序需要知道iPhone是朝上还是朝下/向前还是向后。 你知道这能不能做到吗 > * CMAttitude *attitude; > CMDeviceMotion *motion = motionManager.deviceMotion; > attitude = motion.attitude; 您是否

使用iPhone上的Cmatitude*信息,您可以判断音高。 如果iPhone是垂直的,则为90º。 如果是平的,则为0º。 但我有一个应用程序需要知道iPhone是朝上还是朝下/向前还是向后。 你知道这能不能做到吗

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

您是否尝试过使用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;
     }
 }
当设备改变方向时,这将通知您

支持的方向可在此处找到:

Perfect,当有更改但不是当前状态时,此呼叫会提醒您。我想我可以假设iPhone将面朝上,然后如果它被移动,就可以纠正它。非常感谢。