cocos2d HelloWorld 项目横竖屏、自动旋转问题

首先看下 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));






posted on 2012-09-08 09:35  yang3wei  阅读(519)  评论(0编辑  收藏  举报