iOS 6 - How to run custom code when orientation changes
Asked Answered
E

1

12

I'm creating a game that allows the device to be in either landscape-left or landscape-right orientation, and the player can change the orientation while it's paused. When they do I need to change the way the game interprets the accelerometer based on the orientation.

In iOS 5 I used the willRotateToInterfaceOrientation to catch changes and change my variables, but that's deprecated in iOS6. My existing code looks like this:

    if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)      
        rect = screenRect;

    else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){
        rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width );
    GameEngine *engine = [GameEngine sharedEngine];
    if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){
        engine.orientation = -1;
    } else {
        engine.orientation = 1;
    }
}

I understand that the replacement is the viewWillLayoutSubviews method in the UIViewController class. I'm building this game in cocos2d 2.1 and there doesn't appear to be a UIViewController class in the demo project, so I'm not clear on how to incorporate it and how the code should look in order to make this work.

Ese answered 1/10, 2012 at 21:22 Comment(0)
F
35

Listen for device orientation changes:

[[NSNotificationCenter defaultCenter] 
       addObserver:self
          selector:@selector(deviceOrientationDidChangeNotification:) 
              name:UIDeviceOrientationDidChangeNotification 
            object:nil];

When notified, get the device orientation from UIDevice:

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    switch (orientation)
    {
        // etc... 
    }
}
Ferretti answered 1/10, 2012 at 21:37 Comment(3)
+1 correct option for the given task. Just be careful as you will also get notified on face-up vs. -down orientation changes and mind the difference between UIInterfaceOrientation... and UIDeviceOrientation...America
@America - by face-up versus down do you mean when it orients into portrait or portrait upside-down? Or something else? Also, I assumed DeviceOrientation and InterfaceOrientation are almost always the same except under special circumstances such as multiple views, or if the InterfaceOrientation has been set programmatically not to match the device orientation. Or are there other instances to look out for that might cause an issue?Ese
@JamesMorrison Device orientation and interface orientation are two different enums and two different concepts. Interface orientation is controlled by software (in response to the device orientation); e.g., your software orientation will only ever be landscape-left or landscape-right. Device orientation is controlled by hardware and can be anything at any time, including orientations that have no mapping to interface orientation, like face-up or face-down. You just need to be mindful of the fact that there are many device orientation changes that you'll need to ignore.Ferretti

© 2022 - 2024 — McMap. All rights reserved.