Ios6 support for cocos2d-iphone 2.x screen orientation

Source: Internet
Author: User
========================================================== =============================== 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.