Edward Hasted
Edward Hasted

Reputation: 3433

How do you tell which way up the iPhone is?

Using the CMAttitude* information from an iPhone you can tell the pitch. It the iPhone is vertical it's 90º. If it's flat it's 0º. But I have an app that need to know if the iPhone is facing up or down/forwards or backwards. Any idea if this can be done?

>     * CMAttitude *attitude;
>       CMDeviceMotion *motion = motionManager.deviceMotion;
>       attitude = motion.attitude;

Upvotes: 0

Views: 64

Answers (1)

Casey
Casey

Reputation: 26

Have you tried checking device orientation by using UIDevice class?

Edited: First add an observer to monitor orientation change:

- (void)viewDidLoad
{
    [super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(orientationDidChange:)
                                                 name:UIDeviceOrientationDidChangeNotification object:nil];
}

In the callback, obtain current device orientation

 - (void)orientationDidChange:(NSNotification*)notification
 {
     /// check current orientation
     switch ([[UIDevice currentDevice] orientation])
     {
         case UIDeviceOrientationPortrait:
             break;
         case UIDeviceOrientationPortraitUpsideDown:
             break;
         case UIDeviceOrientationLandscapeLeft:
             break;
         case UIDeviceOrientationLandscapeRight:
             break;
         case UIDeviceOrientationFaceUp:
             break;
         case UIDeviceOrientationFaceDown:
             break;
         default: /// UIDeviceOrientationUnknown
             break;
     }
 }

This will notify you whenever the device changes it's orientation.

Supported orientations can be found here: https://developer.apple.com/library/ios/documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

Upvotes: 1

Related Questions