Pages

Wednesday, March 6, 2013

get Orientation of iPhone/iPad at runtime


Define previousOrientation first, as below in your class.h file


UIDeviceOrientation previousOrientation;

then write code in class.m as below to check the orientation of device. Remember Its not for accelerometer. 


- (id)init {
    
    if ((self=[super init])) {
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(orientationChanged: )
                                                    name:@"UIDeviceOrientationDidChangeNotification" object:nil];
    }
}

- (void) orientationChanged:(NSNotification *)notification{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    
    previousOrientation = orientation;
    if (previousOrientation == UIDeviceOrientationPortrait)
    {
        NSLog(@"PORTRAIT");
    }
    else if (previousOrientation == UIDeviceOrientationLandscapeLeft)
    {
        NSLog(@"LANDSCAPE LEFT");
    }
    else if (previousOrientation == UIDeviceOrientationLandscapeRight)
    {
        NSLog(@"LANDSCAPE RIGHT");
    }
    else
    {
        NSLog(@"PORTRAIT DOWN");
    }
}


No comments:

Post a Comment