首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >修正当前CMMotionManager的UIInterfaceOrientation偏航

修正当前CMMotionManager的UIInterfaceOrientation偏航
EN

Stack Overflow用户
提问于 2016-06-20 23:21:55
回答 1查看 500关注 0票数 0

我正在使用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?

我也需要纠正景观左和右。

代码语言:javascript
复制
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;
        }
    })
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-22 02:23:38

结果是,CMMotionManager为当前的UIInterfaceOrientation定位。因此,最简单的解决方案是在设备旋转时停止并重新启动CMMotionManager。(真希望我早知道这件事,这样我就可以省去很多挫折了!)例如:

代码语言:javascript
复制
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
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37933299

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档