利用controller使cocos2d自动支持180度旋转

本文介绍如何使cocos2d开发的应用在iPad上支持180度屏幕旋转。通过创建一个自定义控制器TransController,并重写shouldAutorotateToInterfaceOrientation方法来实现屏幕旋转功能。同时,对appdelegate进行修改确保应用始终正确地设置设备方向。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值