2

我正在开发 iPhone 游戏,我很想知道当设备强制游戏自动旋转时是否会调用回调函数,以便我可以更改 HUD 元素。

或者我运行无限循环来检查应用程序是否在不同的线程中旋转?问题是我不认为这是一种有效的方法。有没有人对此有任何好的解决方案。

4

2 回答 2

2

您所要做的就是:

在你didfinishlaunching写这个

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

然后复制下面的回调方法

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

    if (orientation == UIDeviceOrientationLandscapeLeft)
    {
        NSLog(@"Landscape Left!");
        self.isLandscapeLeft=1;
    }else if(orientation == UIDeviceOrientationLandscapeRight){
         NSLog(@"Landscape Right!");
        self.isLandscapeLeft=0;
    }
}

同样,您可以检查肖像模式的方向,面朝上,面朝下,倒置以及横向左和横向右方向。:)

于 2012-05-24T05:44:04.457 回答
1

是的,只要设备未锁定方向,您就可以注册收听

UIDeviceOrientationDidChangeNotification

可以在这里看到http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html

如果您想克服设备被锁定的可能性,那么您必须手动监控设备的加速度计。

在需要定位的游戏中,建议手动监控,因为第一种方法有一点延迟。

于 2012-05-24T05:45:31.607 回答