我有一个简单的 Swift 运动跟踪应用程序,可以拉动手机的三轴旋转速率和姿态。我正在尝试使用乘法(byInverseOf:(将初始态度作为参考来规范化态度。当前初始态度返回 0,0,0。在我看来控制器类我有以下内容:
var results : [(rotationRateX: Double, rotationRateY: Double, rotationRateZ: Double, pitch: Double, roll: Double, yaw:Double)] = []
var motionManager = CMMotionManager()
var attitudes : [(CMAttitude)] = []
func startCollectingData()
{
motionManager.deviceMotionUpdateInterval = 1.0 / 100
motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: OperationQueue.current!) { (data, error) in
if let myData = data
{
let rotationRateX = (myData.rotationRate.x)
let rotationRateY = (myData.rotationRate.y)
let rotationRateZ = (myData.rotationRate.z)
let attitude = (myData.attitude)
self.attitudes += [(attitude)]
let initialAttitude = self.attitudes.first
attitude.multiply(byInverseOf: initialAttitude!)
let pitch = attitude.pitch
let roll = attitude.roll
let yaw = attitude.yaw
self.results += [(rotationRateX:rotationRateX, rotationRateY:rotationRateY, rotationRateZ:rotationRateZ, pitch:pitch, roll:roll, yaw:yaw)]
}
}
}
当我尝试先拉动 initialAttitude,在 deviceMotionUpdates 之前(如下所示(,它以 nil 出现。
var results : [(rotationRateX: Double, rotationRateY: Double, rotationRateZ: Double, pitch: Double, roll: Double, yaw:Double)] = []
var motionManager = CMMotionManager()
func startCollectingData()
{
let initialAttitude = motionManager.deviceMotion!.attitude
motionManager.deviceMotionUpdateInterval = 1.0 / 100
motionManager.startDeviceMotionUpdates(using: CMAttitudeReferenceFrame.xArbitraryZVertical, to: OperationQueue.current!) { (data, error) in
if let myData = data
{
let rotationRateX = (myData.rotationRate.x)
let rotationRateY = (myData.rotationRate.y)
let rotationRateZ = (myData.rotationRate.z)
attitude.multiply(byInverseOf: initialAttitude!)
let pitch = attitude.pitch
let roll = attitude.roll
let yaw = attitude.yaw
self.results += [(rotationRateX:rotationRateX, rotationRateY:rotationRateY, rotationRateZ:rotationRateZ, pitch:pitch, roll:roll, yaw:yaw)]
}
}
}
当我尝试先拉 initialAttitude,在 deviceMotionUpdates 之前(如下所示(,它由 nil 出现。
对,所以你只是不断扔掉这些结果,直到你第一次得到一个不nil
的结果。