私はCMMotionMangerを使用して、以下のコードでヨーレートを取得しています。現在のUIInterfaceOrientationの正しいCMMotionManagerヨーを
UIInterfaceOrientationとは無関係に、私はデバイスが90度左にヨーイングされている場合(必要に応じて反転できるので極性に関係しない)、右に90度ヨーイングされた場合は1.5708rad、デバイスが左に90度ヨーイングした場合はプラス1.5708radを得ようとしています。
デバイスが縦向きになっているときに、私が望むことをすることができます。ラジアンでは、デバイスを右に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;
}
})
}