在ios中测量左,右,前和后的倾斜度

时间:2014-11-27 09:19:49

标签: ios cmmotionmanager

我想在所有方面(左,右,前,后)找到倾斜

我使用下面的代码来查找左右

[motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *deviceMotion, NSError *error)
        {
            if (deviceMotion.attitude.roll <= -1)
            {
                [self TiltControl:@"left"];
            }
            else if(deviceMotion.attitude.roll > 1)
            {
                [self TiltControl:@"right"];
            }
        }];

现在如何找到前进和后退...

找到所有4个倾斜的最佳方法是什么......

1 个答案:

答案 0 :(得分:0)

只需使用attitude.pitch属性:

if(deviceMotion.attitude.pitch > 0 ) {
    NSLog(@"forward");
}
else if(deviceMotion.attitude.pitch < 0) {
    NSLog(@"backward");
}

绕Z轴也有旋转,可用attitude.yaw。致NSHipster