我正在使用CMMotionManger获取以下代码的偏航读数。
不管UIInterfaceOrientation,我试图得到读数- 1.5708拉德,如果哈欠90度右,积极1.5708拉德,如果设备是打哈欠90度左(不关心极性,因为它可以逆转,视需要)。
我能够让它做我想做的,当设备是在纵向方向。在弧度上,它给了我大约-1.5708,当设备向右打了90度,当旋转到左边的时候,大约是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;
}
})
}发布于 2016-06-22 02:23:38
结果是,CMMotionManager为当前的UIInterfaceOrientation定位。因此,最简单的解决方案是在设备旋转时停止并重新启动CMMotionManager。(真希望我早知道这件事,这样我就可以省去很多挫折了!)例如:
public override func willRotateToInterfaceOrientation(toInterfaceOrientation: UIInterfaceOrientation, duration: NSTimeInterval) {
stopMotionUpdates()
motionManager = nil
startMotionUpdates()
}
func stopMotionUpdates() {
motionManager?.stopMagnetometerUpdates()
motionManager?.stopDeviceMotionUpdates()
motionManager?.stopAccelerometerUpdates()
}
func startMotionUpdates() {
//Start motion updates is the code in the question above
}https://stackoverflow.com/questions/37933299
复制相似问题