开发者

iPhone SDK - shouldAutorotateToInterfaceOrientation Question

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 ro开发者_如何学运维tate 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 UIDeviceOrientationDidChangeNotification notification and then getting the current orientation using the orientation property of the UIDevice singleton. You will have to start and stop these notifications by calling beginGeneratingDeviceOrientationNotifications and endGeneratingDeviceOrientationNotifications on the UIDevice object.

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 CoreMotion framework and follow the gyro-meter data.


You could register an event and fire it from inside shouldAutorotateToInterfaceOrientation with the needed orientation, while always returning NO.

Disclaimer: Might sound a bit hacky, but it will give you manual control over the interface while changing orientation.


shouldAutorotateToInterfaceOrientation isn't an event. It's simply meant to take a UIInterfaceOrientation and return YES or NO depending on whether or not autorotation is supported.

The Apple docs show that UIInterfaceOrientation is defined as:

   typedef enum {
    UIInterfaceOrientationPortrait           = UIDeviceOrientationPortrait,
    UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
    UIInterfaceOrientationLandscapeLeft      = UIDeviceOrientationLandscapeRight,
    UIInterfaceOrientationLandscapeRight     = UIDeviceOrientationLandscapeLeft 
    }UIInterfaceOrientation;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜