0

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

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

1 回答 1

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

于 2014-08-15T01:24:04.963 回答