========================================================== =============================== Original blog, reprinted please declare the source of Electronic coffee (original id blue rock) ========================================================== ================================
In cocos2d1. X, we set the direction of ctor:
[[Director sharedDirector] setDeviceOrientation (ccDeviceOrientation)orientation];
In cocos2d 2.x, setdeviceorientation is no longer in use. Instead, use the following method:
* In didfinishlaunchingwitexceptions of appdelegate, set delegate to self and override the following method:
- (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 is not used in ios6 and later versions, but is replaced by the following two methods:
// Determine whether the UI will rotate with the device. Execute-(bool) shouldautorotate {return no;} before supportedinterfaceorientations. // set the device direction supported by the UI, which must be info. plist's target settings are the same. // execute-(nsuinteger) supportedinterfaceorientations {// return values; return uiinterfaceorientationmaskall;} Only when shouldautorotate returns yes ;}
Therefore, we need to make corresponding modifications,
1. Add the ccdirectordelegate method to ccprotocols. h:
@ 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 suppsupports the specified orientation. default value is yes (supports all possible orientations) */-(bool) shouldautorotatetointerfaceorientation :( uiinterfaceorientation) interfaceorientation;/** for iOS 6 + Add two methods here */-(bool) shouldautorotate;-(nsuinteger) supportedinterfaceorientations;
2. Override the above two methods in 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. Finally, rewrite in appdelegate:
//support ios 6+-(BOOL)shouldAutorotate{ NSLog(@"AppDelegate--------shouldAutorotate"); return NO;}- (NSUInteger)supportedInterfaceOrientations { NSLog(@"AppDelegate--------supportedInterfaceOrientations"); return UIInterfaceOrientationMaskLandscapeLeft;}
4. The appdelegate rootview will be modified.
[window_ setRootViewController:navController_];
OK. modification is complete.