0

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...

Rajesh
  • 359
  • 3
  • 14

1 Answers1

0

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.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • ciuba: its not working friend... it always shows "forward" & i think default pitch value shows 1.6 and its never come -ve value. – Rajesh Nov 27 '14 at 11:34
  • What is the phone's position, is it laying on a table? In this case the pitch value won't change. Also you can try changing the conditions `deviceMotion.attitude.pitch > 0` if they're not suitable for your needs. – Michał Ciuba Nov 27 '14 at 11:55