iphone rotate ui to orientation

I found a lot of confusing descriptions on how to get iphone views rotated to orientation. Most of them were using interface builder which doesn't help me as most of the applications I build are completely custom and not reliant upon the iphones pre-built components. So this is how I got it to work for my needs.

First I create a notification handler for orientation changes that calls a method in this instance it's 'didRotate'.

[code lang="c"]
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didRotate:)
name:@"UIDeviceOrientationDidChangeNotification"
object:nil];
[/code]

Then in the method, I collect the device orientation and make my necessary changes based on the new orientation.

[code lang="c"]
- (void) didRotate:(NSNotification *)notification{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

if (orientation == UIDeviceOrientationLandscapeLeft)
{
NSLog(@"Landscape Left!");
//landscape left code goes here
//interfaceView.transform = CGAffineTransformMakeRotation(180*M_PI/180);
}
else if (orientation == UIDeviceOrientationLandscapeRight)
{
NSLog(@"Landscape Right!");
//landscape right code goes here
//interfaceView.transform = CGAffineTransformMakeRotation(0*M_PI/180);
}
}
[/code]

It's that easy!

Leave a Reply

Your email address will not be published. Required fields are marked *