AechoLiu
AechoLiu

Reputation: 18408

About the orientation of iPhone

How to get the current orientation of iPhone? I surfed this site, and found two methods as followings.

Which one is the right way to get the current orientation ? I tried two methods under simulator 4.1, but there are some problems for both methods.

Upvotes: 1

Views: 1198

Answers (2)

Hoang Pham
Hoang Pham

Reputation: 6949

Register your class to listen to UIDeviceOrientationDidChangeNotification then handle the device orientation accordingly.

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(deviceRotated:)
name:UIDeviceOrientationDidChangeNotification
object:[UIDevice currentDevice]];

and handle the device's orientation properly:

- (void)deviceRotated: (id) sender{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    if (orientation == UIDeviceOrientationFaceUp ||
        orientation == UIDeviceOrientationFaceDown)
    {
        //Device rotated up/down
    }

    if (orientation == UIDeviceOrientationPortraitUpsideDown)
    {
    }
    else if (orientation == UIDeviceOrientationLandscapeLeft)
    {
    }
    else if (orientation == UIDeviceOrientationLandscapeRight)
    {
    }
}

Upvotes: 6

grahamparks
grahamparks

Reputation: 16296

[[UIDevice currentDevice] orientation] gets the current physical orientation of the device. [UIApplication sharedApplication].statusBarOrientation gets the orientation of the UI. If the app ever returns NO to the shouldAutorotateToInterfaceOrientation: method, the two values will not be the same.

Upvotes: 3

Related Questions