Brett
Brett

Reputation: 12007

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 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

Upvotes: 0

Views: 1152

Answers (3)

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

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.

Upvotes: 1

Narayanan
Narayanan

Reputation: 785

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;

Upvotes: 0

Tudor
Tudor

Reputation: 4164

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.

Upvotes: 0

Related Questions