iOS 6 – 如何在方向更改时运行自定义代码

我正在创build一个游戏,让设备处于横向或横向的方向,玩家可以在暂停时更改方向。 当他们这样做时,我需要改变游戏根据方向来解释加速计的方式。

在iOS 5中,我使用了willRotateToInterfaceOrientation来捕获更改并更改我的variables,但在iOS6中已弃用。 我现有的代码如下所示:

if(toInterfaceOrientation == UIInterfaceOrientationPortrait || toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) rect = screenRect; else if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight){ rect.size = CGSizeMake( screenRect.size.height, screenRect.size.width ); GameEngine *engine = [GameEngine sharedEngine]; if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft){ engine.orientation = -1; } else { engine.orientation = 1; } } 

我明白,replace是UIViewController类中的viewWillLayoutSubviews方法。 我在cocos2d 2.1中构build了这个游戏,并且在演示项目中似乎没有UIViewController类,所以我不清楚如何合并它以及代码应该如何工作。

听取设备方向更改:

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

通知时,从UIDevice获取设备方向:

 - (void)deviceOrientationDidChangeNotification:(NSNotification*)note { UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation]; switch (orientation) { // etc... } }