Callback method for automatic rotation, iOS and iPad

I am working on an iPhone game, I was curious to find out if there is a tht callback function called when the device forces the game to rotate automatically so that I can change the HUD elements.

Or shd starts an infinite loop that checks if the application has changed in another thread? the problem is that I do not think this is an effective way. Anyone have a good solution for this.

+2
source share
2 answers

Yes, there is, while the device is not locked by orientation, you can register to listen

UIDeviceOrientationDidChangeNotification

which can be seen here http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

If you want to overcome the possibility of blocking the orientation of the device, you need to manually control the accelerometer of the device.

In a game where orientation is important, manual monitoring is recommended, as the first method has a slight delay.

+1
source

All you have to do is the following:

In didfinishlaunching write

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

and then copy the following callback method

 - (void) didRotate:(NSNotification *)notification { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; if (orientation == UIDeviceOrientationLandscapeLeft) { NSLog(@"Landscape Left!"); self.isLandscapeLeft=1; }else if(orientation == UIDeviceOrientationLandscapeRight){ NSLog(@"Landscape Right!"); self.isLandscapeLeft=0; } } 

In the same way, you can check the orientation for the wipe mode, face up, face down, upside down, as well as landscape and landscape orientation. :)

+2
source

All Articles