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?
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.
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.
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.
[[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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With