Gamecenter authentication in landscape only Cocos2d with CCLayer for iOS 6

后端 未结 1 1887
我寻月下人不归
我寻月下人不归 2021-01-24 19:06

I\'m having what seems to be a fairly common problem, but my searches and implementations of solutions have not worked out.

I\'ve built a Cocos2d game that is intended

相关标签:
1条回答
  • 2021-01-24 19:48

    This frustrated me for awhile too. After digging around for awhile on the 'Net I found a couple of sources and some worked with iOS 6, some with iOS5, but I had to make some modifications so that it worked the way I wanted on both iOS5 and iOS6. This is the code I am using, it works on my iPhone using 5.1 and 6. Note that the Game Center login still comes up in portrait orientation, there doesn't appear to be anything you can do about that. But the rest of the game will remain in landscape mode.

    1. enable portrait mode as a supported orientation in your build settings (info.plist).
    2. Create a new subclass of UINavigationController. Name this class whatever makes sense to you.
    3. In your AppDelegate, include your new custom UINavigationController header file.
    4. In your App Delegate, comment out the original call and instead call your custom class.

    That should do the trick. Here is the code from my custom class:

    #import <UIKit/UIKit.h>
    
    @interface CustomNavigationViewController : UINavigationController
    
    -(UIInterfaceOrientation) getCurrentOrientation;
    
    @end
    

    And the implementation file:

    #import "CustomNavigationViewController.h"
    
    @interface CustomNavigationViewController ()
    
    @end
    
    @implementation CustomNavigationViewController
    
    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    // This is required to allow GameCenter to login in portrait mode, but only allow landscape mode for the rest of the game play/
    // Arrrgg!
    
    -(BOOL) shouldAutorotate {
        return YES;
    }
    
    -(NSUInteger) supportedInterfaceOrientations {
        return UIInterfaceOrientationMaskLandscape;
    }
    
    -(UIInterfaceOrientation) preferredInterfaceOrientationForPresentation {
        return UIInterfaceOrientationLandscapeRight; // or left if you prefer
    }
    
    -(NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
            return UIInterfaceOrientationMaskLandscape;
        else {
            return UIInterfaceOrientationMaskAllButUpsideDown;
        }
    }
    
    -(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
        return [[UIDevice currentDevice] orientation] != UIInterfaceOrientationPortrait;
    }
    
    -(UIInterfaceOrientation) getCurrentOrientation {
        return [[UIDevice currentDevice] orientation];
    }
    
    @end
    

    Note that last method getCurrentOrientation isn't required I just put that in there in case I wanted to determine what the current orientation is.

    The custom class is called in AppDelegate.m like this: (comment out the original code)

    navController = [[CustomNavigationViewController alloc] initWithRootViewController:director];
    window.rootViewController = navController;
    navController.navigationBarHidden = YES;
    [window makeKeyAndVisible];
    

    Hope this helps.

    0 讨论(0)
提交回复
热议问题