使用CMMotionManager测量倾斜角度 [英] Measuring tilt angle with CMMotionManager

查看:973
本文介绍了使用CMMotionManager测量倾斜角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您在屏幕朝向您的前方垂直握住iphone / ipad,纵向。您将设备倾斜到一侧,使屏幕朝向您。如何使用CMMotionManager测量静态倾斜角度?这似乎是一个简单的问题应该有一个简单的答案,但我找不到任何不会消失在四元数和旋转矩阵中的方法。

Suppose you are holding an iphone/ipad vertically in front of you with the screen facing you, in portrait orientation. You tilt the device to one side, keeping the screen facing you. How do you measure that static tilt angle using CMMotionManager? It seems a simple question which should have a simple answer, yet I cannot find any method that does not disappear into quaternions and rotation matrices.

任何人都可以指出我的工作示例?

Can anyone point me to a worked example?

推荐答案

查看 gravity

self.deviceQueue = [[NSOperationQueue alloc] init];
self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 5.0 / 60.0;

// UIDevice *device = [UIDevice currentDevice];

[self.motionManager startDeviceMotionUpdatesUsingReferenceFrame:CMAttitudeReferenceFrameXArbitraryZVertical
                                                        toQueue:self.deviceQueue
                                                    withHandler:^(CMDeviceMotion *motion, NSError *error)
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        CGFloat x = motion.gravity.x;
        CGFloat y = motion.gravity.y;
        CGFloat z = motion.gravity.z;
    }];
}];

使用此参考框架( CMAttitudeReferenceFrameXArbitraryZVertical ),如果 z 接近于零,则将其保持在与地面垂直的平面上(例如,就像将其靠在墙上一样)并在旋转时将其旋转plane, x y 值更改。 Vertical是 x 接近零且 y 接近-1。

With this reference frame (CMAttitudeReferenceFrameXArbitraryZVertical), if z is near zero, you're holding it on a plane perpendicular with the ground (e.g. as if you were holding it against a wall) and as you rotate it on that plane, x and y values change. Vertical is where x is near zero and y is near -1.

看着这个 post ,我注意到如果你想将这个矢量转换成角度,你可以使用以下算法。

Looking at this post, I notice that if you want to convert this vector into angles, you can use the following algorithms.

如果要计算旋转设备垂直的度数(正向为顺时针,负数为逆时针),则可以将其计算为:

If you want to calculate how many degrees from vertical the device is rotated (where positive is clockwise, negative is counter-clockwise), you can calculate this as:

// how much is it rotated around the z axis

CGFloat angle = atan2(y, x) + M_PI_2;           // in radians
CGFloat angleDegrees = angle * 180.0f / M_PI;   // in degrees

您可以使用它来计算通过Quartz 2D旋转视图的程度 transform property:

You can use this to figure out how much to rotate the view via the Quartz 2D transform property:

self.view.layer.transform = CATransform3DRotate(CATransform3DIdentity, -rotateRadians, 0, 0, 1);

(就个人而言,我更新 startDeviceMotionUpdates 方法,并在 CADisplayLink 中更新此转换,它将屏幕更新与角度更新分离。)

(Personally, I update the rotation angle in the startDeviceMotionUpdates method, and update this transform in a CADisplayLink, which decouples the screen updates from the angle updates.)

您可以通过以下方式查看您向后/向后倾斜的距离:

You can see how far you've tilted it backward/forward via:

// how far it it tilted forward and backward

CGFloat r = sqrtf(x*x + y*y + z*z);
CGFloat tiltForwardBackward = acosf(z/r) * 180.0f / M_PI - 90.0f;

这篇关于使用CMMotionManager测量倾斜角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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