首先看下 GameConfig.h 中的宏定义,配置:
/** * ModifIEd by Bruce Yang on 2012.09.06.13.51~ *//** Supported autorotations: None,UIVIEwController,CCDirector */#define kGameautorotationNone 0#define kGameautorotationCCDirector 1#define kGameautorotationUIVIEwController 2/** define here the type of autorotation that you want for your game~ *//** * 3rd generation and newer devices: * Rotate using UIVIEwController. Rotation should be supported on iPad apps. * TIP: * To improve the performance,you should set this value * to "kGameautorotationNone" or "kGameautorotationCCDirector" */#if defined(__ARM_NEON__) || TARGET_IPHONE_SIMulATOR#define GAME_autoROTATION kGameautorotationUIVIEwController/** * ARMv6 (1st and 2nd generation devices): * Don't rotate. It is very expensive */#elif __arm__#define GAME_autoROTATION kGameautorotationNone/** Ignore this value on Mac~ */#elif defined(__MAC_OS_X_VERSION_MAX_ALLOWED)/** UnkNown architecture~ */#else#error(unkNown architecture)#endif如上,定义了三种控制自动旋转的方式:
1。禁用旋转
2。cocos2d 机制的自动旋转控制
3。ios sdk 自身机制的自动旋转控制
默认情况下会采用 ios sdk 自身机制的自动旋转控制~
(项目设计的时候首先决定有无需求做自动旋转,没有的话就用 kGameautoroationNone
有的话再决定从 kGameautorotationUIVIEwController 和 kGameautorotationCCDirector 里面选一个。
后面二者的区别:
CCDirector 是那种直来直往的,屏幕一翻转它会一步到位将画面翻转过来。
UIVIEwController 则不一样,旋转的时候他会有个过渡的旋转效果,个人感觉比 CCDirector 那种的体验要好。
不过后者的耗费要比前者高。
另外一个要注意的地方就是,CCDirector 不会对 ui 部件产生影响,举个例子来说明:
如果场景里面有个 UIAlertVIEw 对话框,你旋转屏幕之后 UIAlertVIEw 并不会跟着旋转
那样的话 对话框里面的内容将会倒着呈现。
而 UIVIEwController 那种就比较聪明,它在将屏幕画面旋转的同时,也会将对话框翻转一下~
------------------------------------------ 分割线 --------------------------------------------
下面接着说一下怎么控制横屏竖屏
将视线的焦点转移到 RootVIEwController.mm 文件
/** * OverrIDe to allow orIEntations other than the default portrait orIEntation. * 2012.09.06.14.30,今天仔细看了下旋转这块,比想象中的容易多了,哎~ * 主要还是用默认的 kGameautorotationUIVIEwController * 效果比 CCDirector 好(有一个旋转的过渡,而且能让 UI 部件跟着其反应)~ * CCDirector 的话,一下子就翻转了,没有过度,而且不能对 UI 部件如 UIAlertVIEw 做旋转调整~ * 而且,CCDirector 不处理 potrait 的翻转。 * 最后,如果不想启用自动旋转,将 GameConfig 中的 GAME_autoROTATION 的值赋为: * kGameautorotationNone,将会以 portrait 作为默认的显示方式~ */- (BOol)shouldautorotatetoInterfaceOrIEntation:(UIInterfaceOrIEntation)interfaceOrIEntation { /** Add by Bruce Yang on 2011.10.22.17.27~ */// return NO; /** * There are 2 ways to support auto-rotation: * - The OpenGL / cocos2d way * - Faster,but doesn't rotate the UIKit objects * - The VIEwController way * - A bit slower,but the UiKit objects are placed in the right place */ #if GAME_autoROTATION==kGameautorotationNone /** * EAGLVIEw won't be autorotated. * Since this method should return YES in at least 1 orIEntation,* we return YES only in the Portrait orIEntation */ return (interfaceOrIEntation == UIInterfaceOrIEntationPortrait); #elif GAME_autoROTATION==kGameautorotationCCDirector /** * EAGLVIEw will be rotated by cocos2d * Sample: autorotate only in landscape mode */ if(interfaceOrIEntation == UIInterfaceOrIEntationLandscapeleft) { [[CCDirector sharedDirector] setDeviceOrIEntation:kCCDeviceOrIEntationLandscapeRight]; } else if( interfaceOrIEntation == UIInterfaceOrIEntationLandscapeRight) { [[CCDirector sharedDirector] setDeviceOrIEntation:kCCDeviceOrIEntationLandscapeleft]; } // Since this method should return YES in at least 1 orIEntation,// we return YES only in the Portrait orIEntation return (interfaceOrIEntation == UIInterfaceOrIEntationPortrait); #elif GAME_autoROTATION == kGameautorotationUIVIEwController /** * EAGLVIEw will be rotated by the UIVIEwController * Sample: autorotate only in landscpe mode * return YES for the supported orIEntations */// return (UIInterfaceOrIEntationIsPortrait(interfaceOrIEntation)); return (UIInterfaceOrIEntationIsLandscape(interfaceOrIEntation)); #else#error UnkNown value in GAME_autoROTATION #endif // GAME_autoROTATION // Should not happen return NO;}由之前 GAME_autoROTATION 被定义成哪种自动旋转的控制类型,走入上面那个方法以后
会进入到相应的分支,默认情况下用的是 GAME_autoROTATION = kGameautorotationUIVIEwController 的话,
会从 #elif GAME_autoROTATION == kGameautorotationUIVIEwController 这里开始执行。
上面的代码里面,看下面这两行代码,第一行表示呈现方式为竖屏,第二行表示呈现方式为横屏:
//return (UIInterfaceOrIEntationIsPortrait(interfaceOrIEntation));return (UIInterfaceOrIEntationIsLandscape(interfaceOrIEntation));总结
以上是内存溢出为你收集整理的cocos2d HelloWorld 项目横竖屏、自动旋转问题全部内容,希望文章能够帮你解决cocos2d HelloWorld 项目横竖屏、自动旋转问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)