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

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

概述我正在创建一个游戏,允许设备在左侧或横向的方向,而播放器可以在暂停时改变方向.当我们需要改变游戏根据方向解释加速度计的方式时. 在iOS 5中,我使用willRotateToInterfaceOrientation来捕获更改并更改我的变量,但是这在iOS6中已经不推荐了.我现有的代码如下所示: if(toInterfaceOrientation == UIInterfaceOrientationP 我正在创建一个游戏,允许设备在左侧或横向的方向,而播放器可以在暂停时改变方向.当我们需要改变游戏根据方向解释加速度计的方式时.

在iOS 5中,我使用willRotatetoInterfaceOrIEntation来捕获更改并更改我的变量,但是这在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;    }}

我明白替换是UIVIEwController类中的vIEwWillLayoutSubvIEws方法.我正在cocos2d 2.1中构建这个游戏,并且在演示项目中似乎不是一个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...     }}
总结

以上是内存溢出为你收集整理的iOS 6 – 如何在方向更改时运行自定义代码全部内容,希望文章能够帮你解决iOS 6 – 如何在方向更改时运行自定义代码所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/web/1112087.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存