【问题标题】:Correct CMMotionManager yaw for current UIInterfaceOrientation纠正当前 UIInterfaceOrientation 的 CMMotionManager 偏航
【发布时间】: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


    【解决方案1】:

    事实证明,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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      • 2010-10-17
      • 1970-01-01
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多