IOS orientation change detection 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 have registered the game for orientation notifications via:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; 

But it is too slow - it seems there is about a second delay between turning the phone and actually dismissing the notification. I need a way to INSTANTLY detect changes in device orientation. I tried experimenting with a gyroscope, but I am not familiar with it yet to find out if this is really the solution I'm looking for.

+48
ios objective-c iphone orientation gyroscope
Aug 23 2018-12-12T00:
source share
6 answers

This delay you are talking about is actually a filter to prevent false (unwanted) orientation alerts.

For instant recognition of changes in device orientation, you just need to control the accelerometer yourself.

The accelerometer measures acceleration (taking into account gravity) in all three axes, so you should not have any problems determining the actual orientation.

Some code to get started with the accelerometer can be found here:

How to make an iPhone app - Part 5: Accelerometer

And this nice blog covers the math:

Using an accelerometer

+22
Aug 23 2018-12-12T00:
source share

Add notifier in viewWillAppear function

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



Orientation notifies this feature

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



which in turn calls this function, which processes the moviePlayerController frame orientation

 - (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 delete notification

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



I think this is the fastest and can change the view depending on the orientation

+140
Aug 23 2018-12-12T00:
source share

Why didn’t you use

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

?

Or you can use this

 -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration 

Or that

 -(void) didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation 

Hope this will be helpful)

+21
Aug 23 2018-12-12T00:
source share

There was no good solution for my case handling UIDeviceOrientationDidChangeNotification , as it is called more frequent, and UIDeviceOrientation not always equal to UIInterfaceOrientation due to (FaceDown, FaceUp).

I process it using UIApplicationDidChangeStatusBarOrientationNotification :

 //To add the notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangeOrientation:) //to remove the [[NSNotificationCenter defaultCenter]removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil]; ... - (void)didChangeOrientation:(NSNotification *)notification { UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation; if (UIInterfaceOrientationIsLandscape(orientation)) { NSLog(@"Landscape"); } else { NSLog(@"Portrait"); } } 
+10
Dec 23 '14 at 8:26
source share

Try making changes:

- (void) viewWillLayoutSubviews {}

The code will run every time the orientation changes, as the routines will be displayed again.

+5
Mar 25 '13 at 19:36
source share

@vimal's answer did not provide me with a solution. Orientation does not seem to be the current orientation, but the previous orientation. To fix this, I use [[UIDevice currentDevice] orientation]

 - (void)orientationChanged:(NSNotification *)notification{ [self adjustViewsForOrientation:[[UIDevice currentDevice] orientation]]; } 

Then

 - (void) adjustViewsForOrientation:(UIDeviceOrientation) orientation { ... } 

With this code, I get the current orientation position.

+5
Feb 10 '14 at 9:57
source share



All Articles