如何在iOS 8下调整CMMotionManager数据以进行定位? [英] How do I adjust CMMotionManager data for orientation under iOS 8?

查看:120
本文介绍了如何在iOS 8下调整CMMotionManager数据以进行定位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用CMMotionManager跟踪设备动作,但iOS总是以标准设备方向返回设备动作数据(底部的主页按钮)。

My application uses CMMotionManager to track device motion, but iOS always returns device motion data in the standard device orientation (home button at the bottom).

要获取运动数据进入与我的UIView相同的方向,我将视图从我的视图转换到窗口,如下所示:

To get the motion data into the same orientation as my UIView, I accumulate the view transforms from my view down to the window like this:

CGAffineTransform transform = self.view.transform;
for (UIView *superview = self.view.superview; superview; superview = superview.superview) {
    CGAffineTransform superviewTransform = superview.transform;
    transform = CGAffineTransformConcat(transform, superviewTransform);
}

此转换在iOS 6& 7,但iOS 8改变了旋转模型,现在无论设备如何定向,视图总是返回身份变换(无旋转)。来自运动管理器的数据仍然以标准方向固定。

This transform gets calculated correctly under iOS 6 & 7, but iOS 8 changes the rotation model and now the views always return the identity transform (no rotation) no matter how the device is oriented. The data from the motion manager is still fixed in the standard orientation though.

监控UIDevice旋转通知并手动计算四个变换似乎是在iOS下进行此变换的一种方法8,但它看起来也很糟糕,因为设备方向不一定与我的视图方向一致(即iPhone的上部是通常不支持的设备方向)。

Monitoring UIDevice rotation notifications and manually calculating the four transforms seems like one approach to getting this transform under iOS 8, but it also seems bad since the device orientation won't necessarily match my view's orientation (ie, upside on iPhone is a device orientation not normally supported).

什么是在iOS 8下将CMMotionManager的输出转换为特定UIView的方向的最佳方法是什么?

What's the best way to get the output from CMMotionManager into the orientation of a specific UIView under iOS 8?

推荐答案

我找不到一种直接计算变换的方法,所以我改变了我的代码来计算在我的视图控制器中收到一个willRotateToInterfaceOrientation:消息时手动设置变换,如下所示:

I couldn't find a way to directly calculate the transform, so instead I changed my code to calculate set the transform manually when a willRotateToInterfaceOrientation: message is received in my view controller, like this:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    CGAffineTransform transform;
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            transform = CGAffineTransformMake(
                0, -1,
                1, 0,
                0, 0);
            break;

        case UIInterfaceOrientationLandscapeRight:
            transform = CGAffineTransformMake(
                0, 1,
                -1, 0,
                0, 0);
            break;

        case UIInterfaceOrientationPortraitUpsideDown:
            transform = CGAffineTransformMake(
                -1, 0,
                0, -1,
                0, 0);
            break;

        case UIInterfaceOrientationPortrait:
            transform = CGAffineTransformIdentity;
            break;
    }
    self.motionTransform = transform;
}

这篇关于如何在iOS 8下调整CMMotionManager数据以进行定位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆