I am trying to run some code in my iPhone app when the device is rotated left or right.
Clearly, I can do this using shouldAutorotateToInterfaceOrientation, However, I DO NOT want to rotate the controls/subviews on the view. I can stop the subviews from rotating by returning NO to the above method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
NSLog(@"Should rotate\n");
return YES;
}
When I do this and rotate the device left or right, the event fires as expected. My problem is, when I rotate back from landscape to portrait, the event isn’t fired. This has to be because the rotation was stopped by returning NO before it was rotated to lanscape; so the orientation is still considered portrait. However, if I return YES, the subviews rotate.
Has anyone dealt with this issue? Does anyone know a way to either (1) return YES with shouldAutorotateToInterfaceOrientation while keeping the controls static, or (2) capture the device rotation independent of shouldAutorotateToInterfaceOrientation?
Many thanks,
Brett
If you need to just follow the device rotations, you can do this by listening for
UIDeviceOrientationDidChangeNotificationnotification and then getting the current orientation using theorientationproperty of theUIDevicesingleton. You will have to start and stop these notifications by callingbeginGeneratingDeviceOrientationNotificationsandendGeneratingDeviceOrientationNotificationson theUIDeviceobject.This way you can return YES only for the orientation you want the controls to be in and yet follow device orientation changes.
While the purpose isn’t clear, if this is for a game you should look at
CoreMotionframework and follow the gyro-meter data.