你怎么知道iPhone的哪个方向?

时间:2014-08-14 21:32:42

标签: ios cmmotionmanager

使用来自iPhone的CMAttitude *信息,您可以告诉音调。 iPhone是垂直的90º。 如果它是平的那就是0º。 但我有一个应用程序需要知道iPhone是面向上还是向下/向前或向后。 知道是否可以这样做吗?

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

1 个答案:

答案 0 :(得分: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

相关问题