2016-06-20 1 views
0

私は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; 
     } 
    }) 
} 

答えて

0

それは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 
} 
関連する問題