【问题标题】:iOS get angle of face up orientationiOS获取面朝上方向的角度
【发布时间】:2016-10-02 15:04:45
【问题描述】:

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

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

我不能使用 StatusBarOrientation,因为我有固定的方向。状态栏的方向总是一样的

【问题讨论】:

  • 您是指纵向还是横向?还是您指的是加速度计?
  • 设备方向为 FaceUpOrientation。我指的是手机的加速度计旋转。里士满的解决方案对我有用!谢谢

标签: ios objective-c uideviceorientation


【解决方案1】:

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

【讨论】:

  • 完美!这正是我所需要的!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 2022-06-12
相关资源
最近更新 更多