我正在创建一个游戏,它允许设备处于横向左或横向右方向,并且玩家可以在暂停时更改方向.当他们这样做时,我需要更改游戏根据方向解释加速度计的方式.
I'm creating a game that allows the device to be in either landscape-left or landscape-right orientation, and the player can change the orientation while it's paused. When they do I need to change the way the game interprets the accelerometer based on the orientation.
在 iOS 5 中,我使用 willRotateToInterfaceOrientation 来捕获更改并更改我的变量,但在 iOS6 中已弃用.我现有的代码如下所示:
In iOS 5 I used the willRotateToInterfaceOrientation to catch changes and change my variables, but that's deprecated in iOS6. My existing code looks like this:
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;
}
}
我知道替换的是 UIViewController 类中的 viewWillLayoutSubviews 方法.我正在 cocos2d 2.1 中构建这个游戏,并且在演示项目中似乎没有 UIViewController 类,所以我不清楚如何合并它以及代码应该如何看起来才能使其工作.
I understand that the replacement is the viewWillLayoutSubviews method in the UIViewController class. I'm building this game in cocos2d 2.1 and there doesn't appear to be a UIViewController class in the demo project, so I'm not clear on how to incorporate it and how the code should look in order to make this work.
监听设备方向变化:
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(deviceOrientationDidChangeNotification:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
收到通知后,从 UIDevice 获取设备方向:
When notified, get the device orientation from UIDevice:
- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation)
{
// etc...
}
}
这篇关于iOS 6 - 如何在方向改变时运行自定义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!