Detect device orientation change

Is it possible to detect device orientation change? Maybe thare are some functions like IsPortrait() or IsLandscape() or is it possible to register a callback function for orientation change? That would be great. I need this because I want to support both landscape and portrait modes in my simple app.

Can I find somewhere some kind of c*+ wrapper for common ios functionallity like in-app purchases and gamecenter? A c*+ class with functions like SendScore(char* leaderboard) or OpenLeaderboard(char* name) would be great :slight_smile: If anyone has something like this please share :wink:

Ok I found a solution how to detect ios device orientation change and how to support both portrait and landscape resolution. In case someone needs this then this seems to be working with cocos2d-x 2.0.4 however this might not be the best solution :wink: :

changes in RootViewController.mm:

// Override to allow orientations other than the default portrait orientation.
// This method is deprecated on ios6
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return UIInterfaceOrientationIsLandscape( interfaceOrientation ) || UIInterfaceOrientationIsPortrait(interfaceOrientation);
}

// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
- (NSUInteger) supportedInterfaceOrientations{
#ifdef __IPHONE_6_0
    return UIInterfaceOrientationMaskAll;
#endif
}

in AppController.h:

@interface AppController : NSObject  {
    UIWindow *window;
    RootViewController    *viewController;
}

-(void) orientationDidChanged:(NSNotification*)notification;

@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) RootViewController *viewController;

@end

in AppController.mm:

[...]
[[UIApplication sharedApplication] setStatusBarHidden: YES];

    cocos2d::CCApplication::sharedApplication()->run();

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

    return YES;
}

-(void) orientationDidChanged:(NSNotification*)notification
{
    UIDeviceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
    {
        cocos2d::CCSize s = cocos2d::CCEGLView::sharedOpenGLView()->getFrameSize();
        if (s.width < s.height)
            cocos2d::CCEGLView::sharedOpenGLView()->setFrameSize(s.width, s.height);
        else
            cocos2d::CCEGLView::sharedOpenGLView()->setFrameSize(s.height, s.width);
        cocos2d::CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640, 960, kResolutionShowAll);
    }
    else if (orientation == UIDeviceOrientationLandscapeLeft || orientation == UIDeviceOrientationLandscapeRight)
    {
        cocos2d::CCSize s = cocos2d::CCEGLView::sharedOpenGLView()->getFrameSize();
        if (s.width > s.height)
            cocos2d::CCEGLView::sharedOpenGLView()->setFrameSize(s.width, s.height);
        else
            cocos2d::CCEGLView::sharedOpenGLView()->setFrameSize(s.height, s.width);
        cocos2d::CCEGLView::sharedOpenGLView()->setDesignResolutionSize(640, 960, kResolutionShowAll);
    }

}
[...]
1 Like

@neno Did this work for you as expected ? I need it as well.

1 Like

I’ve been struggling to get this to work as well. Android, great – iOS, every single release changes the orientation API -_-

Hi @TheChuckster, @jarsj

orientationDidChanged Notification will trigger only When Device is in Unlocked State(i.e Portrait Lock disabled) & Rotate Device to Landscape or Portrait.
If Device is in Portrait Locked then [[UIDevice currentDevice] orientation] will be always UIDeviceOrientationPortrait. If there is any changes in Device Orientation we won’t get any notification callback.

I have Used Accelerometer Value to Find out the Orientation.

if (fabs(AccelerometerData.acceleration.y) <  fabs(AccelerometerData.acceleration.x))
{
    if(AccelerometerData.acceleration.x > 0)
        currentOrientaion = 2; //UIDeviceOrientationLandscapeLeft
    else
        currentOrientaion = 3; // UIDeviceOrientationLandscapeRight
}
else
{
    if(AccelerometerData.acceleration.y > 0)
        currentOrientaion = 4;  //    UIDeviceOrientationPortraitUpsideDown
    else
        currentOrientaion = 1;   //UIDeviceOrientationPortrait
 }  

Regards,
Gurudath

1 Like

That’s one way of doing it (sad that it comes down to that – I bet it rapidly ping pongs between orientations if it’s flat on the ground, easy fix is to threshold it).

Yes.

When it is flat on the ground we can see changes in Orientation Value.
I have Taken Difference Between Acceleration Y & X Value and fixed it by using threshold value.