iOS 6 - How to run custom code when orientation changes(iOS 6 - 如何在方向改变时运行自定义代码)
问题描述
我正在创建一个游戏,它允许设备处于横向左或横向右方向,并且玩家可以在暂停时更改方向.当他们这样做时,我需要更改游戏根据方向解释加速度计的方式.
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 - 如何在方向改变时运行自定义代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iOS 6 - 如何在方向改变时运行自定义代码
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01