iPhone倾斜角度 - 使用陀螺仪?

gee*_*eks 0 angle pitch ios gyroscope

我想知道iPhone在倾斜时倾斜了多少度 - 也就是说,如果我将iPhone指向上方,我想知道它看起来有多少度?我想,我可以使用陀螺仪和核心运动对于这一点,我认为它是沥青,我想,但是,间距是每秒弧度"速率".我对用户移动iPhone的速度并不感兴趣,我只对倾斜角度感兴趣.所以,如果我指着手机拍照,我想知道指向的角度 - 任何想法?

谢谢.

And*_* G. 8

陀螺仪数据以弧度/秒为单位,但您要查找的是CMMotionManager.attitude属性.它以弧度表示相对于某个参照系的物体的姿态.

创建类变量motionManager和init:

 motionManager = [[CMMotionManager alloc] init];
    motionManager.deviceMotionUpdateInterval = 0.1f;
    [motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
        [self processMotion:motion];
    }];
Run Code Online (Sandbox Code Playgroud)

处理更新,您正在寻找音调,但在此示例中,它将显示所有三个值,以便您可以玩并决定您需要的内容:

    -(void)processMotion:(CMDeviceMotion*)motion {
        NSLog(@"Roll: %.2f Pitch: %.2f Yaw: %.2f", motion.attitude.roll, motion.attitude.pitch, motion.attitude.yaw);
}
Run Code Online (Sandbox Code Playgroud)

这些是Euler Angles,您还可以选择获取rotationMatrix或四元数格式.每个都有各自的优缺点.