cocos2d-iphone 2.x 屏幕方向对ios6的支持问题

============================================================
博文原创,转载请声明出处
电子咖啡(原id蓝岩)
============================================================
在cocos2d1.x中,我们设置director的方向用:

[[Director sharedDirector] setDeviceOrientation (ccDeviceOrientation)orientation];


在cocos2d 2.x中,setDeviceOrientation已经不在使用。代替的使用下面的方法:

*在AppDelegate的didFinishLaunchingWithOptions中,设置delegate为self,并重写下面方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
   //do something you need
   [director setDelegate:self];
}
//support device orientation
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return UIInterfaceOrientationIsLandscape(interfaceOrientation);
}

但shouldAutorotateToInterfaceOrientation在IOS6及更高版本不在使用,而是用下面两个方法代替:

//判断ui是否会随设备而旋转,先于supportedInterfaceOrientations执行
-(BOOL)shouldAutorotate{
    return NO;
}
//设置ui支持的设备方向,需要和info.plist的target设置一样,
//在shouldAutorotate返回YES时候才回执行
- (NSUInteger)supportedInterfaceOrientations {
//    return UIInterfaceOrientationMaskLandscapeLeft;
//    return UIInterfaceOrientationMaskLandscapeRight;
    return UIInterfaceOrientationMaskAll;
}
因此我们需要做对应修改,
1、CCProtocols.h中添加CCDirectorDelegate方法:

@protocol CCDirectorDelegate <NSObject>

@optional
/** Called by CCDirector when the porjection is updated, and "custom" projection is used */
-(void) updateProjection;

#ifdef __CC_PLATFORM_IOS
/** Returns a Boolean value indicating whether the CCDirector supports the specified orientation. Default value is YES (supports all possible orientations) */
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
/** for IOS 6+  这里添加两个方法*/
-(BOOL)shouldAutorotate;
- (NSUInteger)supportedInterfaceOrientations ;
2、在CCDirectorIOS.m中覆盖上面两个方法:

-(BOOL)shouldAutorotate{
    BOOL ret =YES;
	if( [delegate_ respondsToSelector:_cmd] )
		ret = (BOOL) [delegate_ shouldAutorotate];
    
	return ret;
}
-(NSUInteger)supportedInterfaceOrientations{
    int ret =UIInterfaceOrientationMaskAll;
	if( [delegate_ respondsToSelector:_cmd] )
		ret = (int) [delegate_ supportedInterfaceOrientations];
    
	return ret;
}

3、最后在AppDelegate中重写:

//support ios 6+
-(BOOL)shouldAutorotate{
    NSLog(@"AppDelegate--------shouldAutorotate");
    return NO;
}
- (NSUInteger)supportedInterfaceOrientations {
    NSLog(@"AppDelegate--------supportedInterfaceOrientations");
    return UIInterfaceOrientationMaskLandscapeLeft;
}

4、最后将修改AppDelegate的rootview

[window_ setRootViewController:navController_];

ok,修改结束


你可能感兴趣的:(cocos2d-iphone 2.x 屏幕方向对ios6的支持问题)