Measuring tilt with left, right, forward and backward in ios

559 views Asked by At

I want to find tilt in all side (left, right, forward, backward)

I have use below code to find left and right

[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"];
            }
        }];

now how can find forward and backward...

what is the best way to find all 4 tilt...

1

There are 1 answers

2
Michał Ciuba On

Just use the attitude.pitch property:

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

There is also rotation around Z axis, available as attitude.yaw. Credits to NSHipster.