views:

166

answers:

2

I have an app with a tab bar, and nav controllers in each tab. When user shakes the device, a UIImageView appears as a child view in the nav controller. But the UIImageView must contain a special image, depending on the device's current orientation.

If I write just

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)
if (interfaceOrientation == UIInterfaceOrientationPortrait|| interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) { 
//Code
}
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight||interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
//Code
}
}

The view just goes crazy if user rotated the device before shaking. Is there a method to get iPhones current orientation?

A: 

Use the [[UIDevice currentDevice] orientation] method, as specified here.

Tim
Thanks, but which values can the orientation be? From -90 til 90 - portrait and from -180 til -90 and from 90 til 180-landscape?
Knodel
See @Shirkrin's answer - it returns a value of type `UIDeviceOrientation`, which is an `enum` of a certain list of values.
Tim
+1  A: 

To addon to the already answered question:

You use [[UIDevice currentDevice] orientation] which will yield one of these values:

typedef enum {
   UIDeviceOrientationUnknown,
   UIDeviceOrientationPortrait,
   UIDeviceOrientationPortraitUpsideDown,
   UIDeviceOrientationLandscapeLeft,
   UIDeviceOrientationLandscapeRight,
   UIDeviceOrientationFaceUp,
   UIDeviceOrientationFaceDown
} UIDeviceOrientation;

The documentation can be found here - (orientation) and here - (UIDeviceOrientation).

(I don't mean to claim the former anwser, but this information was to big for a comment.)

Shirkrin
Much more complete than my answer. Thanks and +1
Tim