| iPhone OS 3.0 and the new autorotation behaviour |
|
|
|
| News - Development |
| Written by Luzian Scherrer |
| Monday, 18 May 2009 22:31 |
|
As others have already pointed out, the new iPhone OS 3.0 (currently in beta5) breaks the known behaviour of the UIViewController's shouldAutorotateToInterfaceOrientation method. As this is critical functionality for our game Twizzle, we had to look into it and luckily we were able to figure out a nice workaround for the problem. Let's shortly explain what the problem actually is: When you rotate from UIInterfaceOrientationPortrait to UIInterfaceOrientationLandscapeLeft the shouldAutorotateToInterfaceOrientation method gets called. Your shouldAutorotateToInterfaceOrientation implementation can then return YES or NO. When you return YES, your UIViewController is automatically rotated. Pretty nice if you need it. But there are other use cases where you want to know about orientation changes although you do not want your view to autorotate because you need to do the rotation yourself. In such a case, you would return NO from shouldAutorotateToInterfaceOrientation. So far so good. In iPhone OS versions 2.x shouldAutorotateToInterfaceOrientation always gets called no matter whether you have returned YES or NO before. In iPhone OS version 3.0 (currently beta 5) this is different: if you return NO from shouldAutorotateToInterfaceOrientation when rotating the device to one side, then shouldAutorotateToInterfaceOrientation does not get called again when you rotate the device back. This means that such a piece of code is not usable any more: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // custom rotation code based on interfaceOrientation here... return NO; } Our solution is to remove this method completey and instead we register for the UIDeviceOrientationChangeNotification on creation of our view controller: [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil]; Now we can use the didRotate method for our custom rotation implementation: -(void)didRotate:(NSNotification *)theNotification { UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation]; // custom rotation code based on interfaceOrientation here... } Pretty simple once you've figured it out. And this solution works with all currently known iPhone OS versions. Twizzle aficionados can happily be looking forward to iPhone OS 3.0!
|



