【发布时间】:2014-10-14 05:38:19
【问题描述】:
我正在尝试复制 Compass 应用程序中的功能 - 但我遇到了一个问题:如何确定界面中“向上”的方式?
我在屏幕上有一个标签,并且我有以下代码,可将其定向为在设备四处移动时保持水平:
self.motionManager = CMMotionManager()
self.motionManager?.gyroUpdateInterval = 1/100
self.motionManager?.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (deviceMotion, error) -> Void in
let roll = -deviceMotion.attitude.roll
self.tiltLabel?.transform = CGAffineTransformRotate(CGAffineTransformIdentity, CGFloat(roll))
})
这个效果还不错,但也有一些地方是错误的——例如,当 iPhone 的闪电接头朝上时,标签会不规律地翻转。
如何使用 CoreMotion 始终如一地判断哪个方向是向上的?
更新: 显然,roll/pitch/yaw 是 Euler angles,它会受到 gimbal lock 的影响 - 所以我认为正确的解决方案可能涉及使用 quaternions,它不会受到影响这个问题,或者 CMAttitude 上的 rotationMatrix 可能会有所帮助:https://developer.apple.com/library/ios/documentation/CoreMotion/Reference/CMAttitude_Class/index.html
【问题讨论】:
标签: ios core-motion