开发者

iPad device orientation always wrong when flat

开发者 https://www.devze.com 2023-03-11 19:33 出处:网络
I know this subject gets a lot of attention here at SO, but none of the \"solutions\" is working for me. Here\'s the situation:

I know this subject gets a lot of attention here at SO, but none of the "solutions" is working for me. Here's the situation:

  1. iPad is flat on my desk (a face up device orientation), the home screen is perfectly readable, but the home button is on top. So this is effectively shows a "Portrait Up Side Down" interface orientation.

  2. I start my app

  3. The app calls beginGeneratingDeviceOrientationNotifications to make sure the accelerometer is working, and the orientation notifications come in.

  4. At startup, my app queries both the "interface orientation" and the "device orientation". The results are:

     STARTUP ORIENTATION:
         INTERFACE ORIENTATION: UIInterfaceOrientationPortrait
         DEVICE ORIENTATION: UIDeviceOrientationFaceUp
    

    The Apple docs say that each app (interface?) starts in portrait orientation, so this could make sense. The device orientation is also correct, because this is indeed face up.

  5. Right after launching, the first orientation notification is coming in:

    NOTIFICATION 1: 
        INTERFACE: UIInterfaceOrientationPortrait   
        DEVICE: UIDeviceOrientationPortrait
    

    Again, portrait for the interface, (which is contrary to what I'd expect, the home screen showed itself in up side down orientation), the device is said to be in portrait too (which isn't so, but this is apparantly a clever scheme to work out how to orient your app's contents when the device is face up or down).

  6. As many people mention, another (just one, not two) orientation notification follows:

    NOTIFICATION 2: 
        INTERFACE: UIInterfaceOrientationPortrait   
        DEVICE: UIDeviceOrientationFaceUp
    

    Sti开发者_运维百科ll portrait (and not up side down) for the interface, and back to face up for the device (which is correct).

My app adjusts itself correctly, and is shown up side down, which is totally wrong.

What can I do to know the last known orientation, the one's that's shown in the home screen and other UI on the iPad?

This is an OpenGL app with just a window and a view. I've already added a UIViewController to the mix to see if this gives more accurate information, but it's just the same, and wrong. I know I'm not the only dev struggling with this, because many apps I have on my iPad do it wrong too, you have to tilt the device to make them orient themselves properly. But some apps do seem to get it right, what's the secret?


Okay, I'm not happy to have to admit that I did overlook an essential piece in the maze that is iOS device/interface orientations: the UISupportedInterfaceOrientations property in your app's Info.plist file.

I read about it and (wrongly) assumed that by specifying the supported orientations, the behavior wouldn't change. But as it turns out, it's not just a filter on the orientation notifications (it isn't, you still get notifications for unsupported orienations), but the type/order of notifications is different depending on how the device is oriented.

In my case, I wasn't interested in the "face up" and "face down" orientations, so I just ignored them. This is not a good idea. If you specify the supported orientations explicitly:

<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>

...then you will still see the behavior as I describe in the comments above:

"Essentially, the device is pretending the user really quickly shook it back to an upright position, and back down."

...but the first passed orientation isn't always portrait, but the correct orientation instead.

So, in the scenario I describe in my question, the device orientations would be as follows:

AT STARTUP: UIDeviceOrientationFaceUp
NOTIFICATION 1: UIDeviceOrientationPortraitUpsideDown <-- CORRECT!
NOTIFICATION 2: UIDeviceOrientationFaceUp <-- CORRECT, BUT IGNORE

I wouldn't want to go as far and call this a bug on Apple's part, but it's unclear to me why the passed orientation in NOTIFICATION 1 is always portrait when you don't specify UISupportedInterfaceOrientations.

Note that this only applies to the iPad. The home screen UI on iPod Touch and iPhone are always in portrait, so it's easier to know what that orientation was at launch (you guessed it: portrait)...

0

精彩评论

暂无评论...
验证码 换一张
取 消