如何在设备方向(不是界面方向)事件上订阅自己?

How to subscribe self on the event of Device Orientation(not interface orientation)?(如何在设备方向(不是界面方向)事件上订阅自己?)

本文介绍了如何在设备方向(不是界面方向)事件上订阅自己?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想在设备旋转(方向改变)的情况下调用 CCScene myscene 中的一些方法.我禁用了自动旋转(因为我不希望它发生).

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?

推荐答案

[[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;

这篇关于如何在设备方向(不是界面方向)事件上订阅自己?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在设备方向(不是界面方向)事件上订阅自己?

基础教程推荐