Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subscribe self on the event of Device Orientation(not interface orientation)?

in my app i want to call some method in CCScene myscene in the case of device rotation(orientation change).I disabled the autorotation(because i want it not to happen).

The issue is: i want to change gravity in the scene depending on my device orientation. My code :

-(void) onEnter
{
    [super onEnter];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notification_OrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void) onExit
{
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    //[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
-(void)notification_OrientationWillChange:(NSNotification*)n
{
    orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
}
-(void)notification_OrientationDidChange:(NSNotification*)n
{
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        b2Vec2 gravity( 0, -10);
        world->SetGravity(gravity);
    }
    if (orientation == UIInterfaceOrientationLandscapeRight) {
        b2Vec2 gravity( 0, 10);
        world->SetGravity(gravity);
    }
}

But in this case i can get notifications only in the case of autorotation enabled.(if it disabled, device is actually don't change it status bar orientation) Can you help me?

like image 662
Alexander Tkachenko Avatar asked Jan 21 '11 11:01

Alexander Tkachenko


People also ask

What is Interface orientation?

Interface Orientation can be anything, regardless of device orientation. Device Orientation is the actual physical orientation of the device you are holding, and this is not variable; it is what it is. If you are holding in Portrait, it is Portrait.

Which method called when orientation changes iOS?

This inherited UIViewController method which can be overridden will be trigger every time the interface orientation will be change. Consequently you can do all your modifications in the latter. By implementing this method you'll then be able to react to any change of orientation to your interface.

What is device orientation in Xcode?

Upside Down: This orientation means you change the orientation of your iOS device 180 degrees. But if you check this checkbox only, you will get the below error message when you run the app. So this checkbox should be used with other orientations such as Portrait or Landscape.


1 Answers

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



- (void)orientationChanged:(NSNotification *)notification{  
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    //do stuff
    NSLog(@"Orientation changed");          
}

You must check for Device orientation not status bar.

typedef enum {
        UIDeviceOrientationUnknown,
        UIDeviceOrientationPortrait,            // Device oriented vertically, home button on the bottom
        UIDeviceOrientationPortraitUpsideDown,  // Device oriented vertically, home button on the top
        UIDeviceOrientationLandscapeLeft,       // Device oriented horizontally, home button on the right
        UIDeviceOrientationLandscapeRight,      // Device oriented horizontally, home button on the left
        UIDeviceOrientationFaceUp,              // Device oriented flat, face up
        UIDeviceOrientationFaceDown             // Device oriented flat, face down
    } UIDeviceOrientation;
like image 136
Alex Terente Avatar answered Oct 21 '22 07:10

Alex Terente