execute method when iPhone enters landscape mode
Is there a delegate that will get called when the iPhone enters landscape or portrait mode? I need to change the style and place objects in a 开发者_运维技巧different place when the iPhone get's rotated. Do I have to do this with the accelerometer? Moreover if there exist such a delegate do I have to create the connection in interface builder. I am new to objective-c...
Register to listen for the orientation change notification.
UIDevice *device = [UIDevice currentDevice];
//Tell it to start monitoring the accelerometer for orientation
[device beginGeneratingDeviceOrientationNotifications];
//Get the notification centre for the app
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification
object:device];
Implement orientationChanged:
method, which will be called when the device change the orientation. you could put code to check the orientation type and called your method.
- (void)orientationChanged:(NSNotification *)note
{
NSLog(@"Orientation has changed: %d", [[note object] orientation]);
}
Remove notification in dealloc
.
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
Check the blog post
Reacting to iPhone's orientation
Implement didRotateFromInterfaceOrientation in your view controller
-(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
You can get UIDevice to generate notifications for orientation events. See the documentation for UIDevice.
If you need to detect the change at any moment, you might consider calling -beginGeneratingDeviceOrientationNotifications
in your app's delegate.
Also, if you are using UIViewController
s, there are
shouldAutorotateToInterfaceOrientation:
willRotateToInterfaceOrientation: duration:
didRotateFromInterfaceOrientation:
精彩评论