Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting iOS orientation change instantly

I have a game in which the orientation of the device affects the state of the game. The user must quickly switch between Landscape, Portrait, and Reverse Landscape orientations. So far I've been registering the game for orientation notifications via:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

But it is far too slow - there seems to be about a second delay between rotating the phone and the notification actually being fired. I need a way to INSTANTLY detect changes in the device's orientation. I have tried experimenting with the gyroscope, but am not yet familiar enough with it to know whether or not it is the solution I am looking for.

like image 215
GoldenJoe Avatar asked Aug 23 '12 06:08

GoldenJoe


People also ask

How to detect orientation change in iOS?

You can do that with cmd+k when you have your code selected.


2 Answers

Add a notifier in the viewWillAppear function

-(void)viewWillAppear:(BOOL)animated{   [super viewWillAppear:animated];   [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)    name:UIDeviceOrientationDidChangeNotification  object:nil]; } 

The orientation change notifies this function

- (void)orientationChanged:(NSNotification *)notification{    [self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]]; } 

which in-turn calls this function where the moviePlayerController frame is orientation is handled

- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {      switch (orientation)     {         case UIInterfaceOrientationPortrait:         case UIInterfaceOrientationPortraitUpsideDown:         {          //load the portrait view             }              break;         case UIInterfaceOrientationLandscapeLeft:         case UIInterfaceOrientationLandscapeRight:         {         //load the landscape view          }             break;         case UIInterfaceOrientationUnknown:break;     } } 

in viewDidDisappear remove the notification

-(void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];    [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; } 

I guess this is the fastest u can have changed the view as per orientation

like image 115
Vimal Venugopalan Avatar answered Sep 22 '22 18:09

Vimal Venugopalan


That delay you're talking about is actually a filter to prevent false (unwanted) orientation change notifications.

For instant recognition of device orientation change you're just gonna have to monitor the accelerometer yourself.

Accelerometer measures acceleration (gravity included) in all 3 axes so you shouldn't have any problems in figuring out the actual orientation.

Some code to start working with accelerometer can be found here:

How to make an iPhone App – Part 5: The Accelerometer

And this nice blog covers the math part:

Using the Accelerometer

like image 38
Rok Jarc Avatar answered Sep 21 '22 18:09

Rok Jarc