iPhone开发:利用controller使cocos2d自动支持180度旋转

iPad程序有一条不成文的规定:所有app必须支持屏幕旋转,至少支持180度旋转,仅仅支持一个方向的app将被无情拒绝。

 

目前cocos2d已经支持iPad开发,却并不支持屏幕旋转。

 

下面我们就利用UIController的自动旋转特性,让cocos2d支持180度旋转:

 

1。创建controller,且叫做TransController吧,内容如下

 

#import <UIKit/UIKit.h>

 

#define LANDSCAPE_MODE YES

 

@interface TransController : UIViewController {

 

}

 

@end

 

 

#import "TransController.h"

 

 

@implementation TransController

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {

    // Overriden to allow any orientation.

if (LANDSCAPE_MODE){

if (interfaceOrientation == UIDeviceOrientationLandscapeLeft||interfaceOrientation == UIDeviceOrientationLandscapeRight) {

return YES;

}

}else {

if (interfaceOrientation == UIDeviceOrientationPortrait||interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) {

return YES;

}

}

return NO;

}

 

- (void)didReceiveMemoryWarning {

    // Releases the view if it doesn't have a superview.

    [super didReceiveMemoryWarning];

 

    // Release any cached data, images, etc that aren't in use.

}

 

 

- (void)viewDidUnload {

    [super viewDidUnload];

    // Release any retained subviews of the main view.

    // e.g. self.myOutlet = nil;

}

 

 

- (void)dealloc {

    [super dealloc];

}

 

 

@end

 

#define LANDSCAPE_MODE YES 用来设定app的横竖屏模式

 

2。修改appdelegate

无论程序是横/竖,setDeviceOrientation都为竖屏,方向总是相对的,呵呵,即总是:

[[CCDirector sharedDirector] setDeviceOrientation:CCDeviceOrientationPortrait];

 

TransController *tc = [[TransController alloc] init];

tc.view.frame = window.frame;

[window addSubview:tc.view];

[[CCDirector sharedDirector] attachInView:tc.view];

 

tc无需释放(因为没有意义),也不可以释放(释放后将不能旋转),如果你喜欢释放的话,就在.h中定义然后dealloc中去release吧。

posted @ 2010-06-23 15:57  javawebsoa  Views(243)  Comments(0Edit  收藏  举报