【发布时间】:2016-06-20 23:21:55
【问题描述】:
我正在使用 CMMotionManger 通过以下代码获取偏航读数。
不管 UIInterfaceOrientation 是什么,如果向右偏航 90 度,我会尝试获得负 1.5708 rad 的读数,如果设备向左偏航 90 度,则读数为负 1.5708 rad(不关心极性,因为它可以根据需要反转)。
当设备处于纵向时,我可以让它做我想做的事情。以弧度表示,当设备向右偏航 90 度时,它给我大约 -1.5708,当向左旋转时,它给我大约 1.5708 弧度。
但是当设备处于纵向倒置方向时,当偏航向右旋转时,它从 -2.4 左右开始下降到 -3.14 左右,然后跳到 ~3.14 下降到 2.6。如何使其平滑连续 0 到 -1.5708 rad?
我还需要校正左右横向。
if motionManager == nil {
motionManager = CMMotionManager()
}
let updateInterval: NSTimeInterval = 1 / 24.0 //24hz
if (motionManager!.accelerometerAvailable) {
motionManager!.accelerometerUpdateInterval = updateInterval
motionManager!.startDeviceMotionUpdatesToQueue(NSOperationQueue.mainQueue(), withHandler: { (motion:CMDeviceMotion?, error: NSError?) -> Void in
print("\(motion!.attitude.yaw)")
switch (TDTDeviceUtilites.interfaceOrientation()) {
case UIInterfaceOrientation.Portrait:
// No correction needed
break;
case UIInterfaceOrientation.PortraitUpsideDown:
//need to apply correction
break;
case UIInterfaceOrientation.LandscapeRight:
//need to apply correction
break;
case UIInterfaceOrientation.LandscapeLeft:
//need to apply correction
break;
}
})
}
【问题讨论】:
标签: ios swift uiinterfaceorientation cmmotionmanager