0

Our app consists of Cocos3D and AR. Since upgrading to iOS 8 we had to fix our orientation handling because of the changes Apple brought on us.

However, we still experience some issues that lead me to a rather interesting phenomena (I checked it in our app and in a fresh new project..):

Logging the [UIDevice currentDevice].orientation,[UIApplication sharedApplication].statusBarOrientation and [UIScreen mainScreen].bounds.size shows that when the device is rotated quickly, the device orientation actually changes, however the interface orientation sometimes stays the same and it leads to wrong calculations of the screen bounds... e.g.:

before rotation:

[UIDevice currentDevice].orientation = 1

[UIApplication sharedApplication].statusBarOrientation = 1

[UIScreen mainScreen].bounds.size = 414x736

after first rotation:

[UIDevice currentDevice].orientation = 4

[UIApplication sharedApplication].statusBarOrientation = 4

[UIScreen mainScreen].bounds.size = 736x414

after second rotation:

[UIDevice currentDevice].orientation = 1

[UIApplication sharedApplication].statusBarOrientation = 4

[UIScreen mainScreen].bounds.size = 736x414

Now obviously, after the second rotation, the device is back in portrait mode - well not according to the interface orientation and the screen bounds...

Am I missing something here?

GalSab
  • 109
  • 2
  • 8
  • Where do you log the result? I mean in which method you are calling the NSLog. – LoVo Apr 02 '15 at 07:33
  • added an observer for `UIDeviceOrientationDidChangeNotification` leading to a method called `orientationDidChange` (my method) – GalSab Apr 02 '15 at 07:38
  • UIDeviceOrientation is not always UIInterfaceOrientation. For example, when your device is on a plain table you can receive unexpected value. – LoVo Apr 02 '15 at 07:41
  • I know that, you're referring to Face-Up and Face-Down orientation. but that's not the case i'm talking about. rotating to landscape from portrait and back while holding the phone in my hand and logging the orientations - I see the changes between the correct orientations and not unexpected ones. – GalSab Apr 02 '15 at 07:45
  • According to this guy (not the marked answer but the most upvoted one) http://stackoverflow.com/questions/634745/how-to-programmatically-determine-iphone-interface-orientation using UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; is the most effective way and usually gives you the correct result – LoVo Apr 02 '15 at 07:47
  • I am using exactly this for my calculations but this is not my problem. I am afraid I wasn't clear enough - the interface orientation (that I use to calculate locations) isn't always changing synchronously with the device's orientation even tho it should. It happens only when the device is rotated fairly quick. The device recognizes the rotation but for some reason it is not applied to the statusBarOroentation... – GalSab Apr 02 '15 at 07:50

1 Answers1

1

I have had this issue in the past and have concluded that when you set up an observer to the UIDeviceOrientationDidChangeNotification the method you specify is called before all attributes change to reflect the new orientation. The way I dealt with it is to create a function which runs a block of code after a delay and then call:

- (void)deviceOrientationDidChange:(NSNotification *)note
{
    RHRunBlockAfterDelay(0.5, ^{
        // Execute relevant code...
    });
}

The delay function is a wrapper around a call to [self performSelector:@selector(mySel) afterDelay:delay withObject:block]; defined in a separate Functions.h.

Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
  • You, sir, nailed it. Thank you very much. Now the shortest delay I could find that works is 0.25. Still noticeable tho but it does the job. – GalSab Apr 03 '15 at 11:52