iOS 获取面朝上的角度

iOS get angle of face up orientation

以 FaceUp 方向加载屏幕时,我需要知道 iPhone 的角度。

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

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

这可能是使用 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)
            }
        })
}