iOS获得面朝上的角度

时间:2016-06-02 18:11:54

标签: ios objective-c uideviceorientation

在FaceUp Orientation中加载屏幕时,我需要知道iPhone的角度。

iPhone平放在桌面上,但我只需要知道它是处于垂直位置还是水平位置。

我无法使用StatusBarOrientation,因为我有固定的方向。状态栏的方向始终相同

1 个答案:

答案 0 :(得分:0)

这可能是使用CoreMotion的好时机。看起来像阅读CoreMotionRate可能会给你你想要的东西:

来自文档:

/*
 *  CMRotationRate
 *  
 *  Discussion:
 *    A structure containing 3-axis rotation rate data.
 *
 *  Fields:
 *    x:
 *      X-axis rotation rate in radians/second. The sign follows the right hand 
 *      rule (i.e. if the right hand is wrapped around the X axis such that the 
 *      tip of the thumb points toward positive X, a positive rotation is one 
 *      toward the tips of the other 4 fingers).
 *    y:
 *      Y-axis rotation rate in radians/second. The sign follows the right hand 
 *      rule (i.e. if the right hand is wrapped around the Y axis such that the 
 *      tip of the thumb points toward positive Y, a positive rotation is one 
 *      toward the tips of the other 4 fingers).
 *      z:
 *          Z-axis rotation rate in radians/second. The sign follows the right hand 
 *      rule (i.e. if the right hand is wrapped around the Z axis such that the 
 *      tip of the thumb points toward positive Z, a positive rotation is one 
 *      toward the tips of the other 4 fingers).
 */

如何获取这些值的快速示例:

private lazy var motionManager: CMMotionManager = {
    return CMMotionManager()
}()

func recordMotion() {
   motionManager.startDeviceMotionUpdatesToQueue(opQueue, withHandler: { (deviceMotion, error) in
            if let motion = deviceMotion {
                 print(motion.rotationRate.x)
                 print(motion.rotationRate.y)
                 print(motion.rotationRate.z)
            }
        })
}