How to let UIViewcontroller (Game center) responds to the Orientation (Landscape or Portrait)?
i used the code below to show the game center but if I rotate the iPhone nothing happen and the Leaderboard will be upside-down (not respond for the orientation)
The code
// Create leaderboard view w/ default Game Center style
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController alloc] init];
// If view controller was successfully created...
if (leaderboardController != nil)
{
// Leaderboard config
leaderboardController.leaderboardDelegate = self; // The leaderboard view controller will send messages to this object
leaderboardController.category = category; // Set category here
leaderboardControll开发者_Python百科er.timeScope = GKLeaderboardTimeScopeAllTime; // GKLeaderboardTimeScopeToday, GKLeaderboardTimeScopeWeek, GKLeaderboardTimeScopeAllTime
// Create an additional UIViewController to attach the GKLeaderboardViewController to
myViewController = [[UIViewController alloc] init];
// Add the temporary UIViewController to the main OpenGL view
// [[[CCDirector sharedDirector] openGLView] addSubview:myViewController.view];
[[[[CCDirector sharedDirector] openGLView] window] addSubview:myViewController.view];
// Tell UIViewController to present the leaderboard
[myViewController presentModalViewController:leaderboardController animated:YES];
leaderboardController.view.transform = CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90.0f));
leaderboardController.view.bounds = CGRectMake(0, 0, 480, 320);
leaderboardController.view.center = CGPointMake(160,240 );
}
Is there any another method to show it? any help plz
Thank you
huh I found maybe a specific solution so I replace the if (leaderboardController != nil){...}
by the code below.
The code :
if (leaderboardController != NULL) {
leaderboardController.category = category;
leaderboardController.timeScope =GKLeaderboardTimeScopeAllTime;
leaderboardController.leaderboardDelegate = self;
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[delegate.viewController presentModalViewController:leaderboardController animated:YES];
}
also need to add a property for the viewController to the AppDelegate.h
First add a property declaration, as shown below:
@property (nonatomic, assign) RootViewController *viewController;
Then switch to AppDelegate.m
and synthesize the variable, as shown down:
@synthesize viewController;
Finally since this code uses UIKit to display a view controller, it’s best to set Cocos2D to use UIViewController rotation.
So go to GameConfig.h, comment out the current #define GAME_AUTOROTATION kGameAutorotationNone
, and set it up as shown:
#define GAME_AUTOROTATION kGameAutorotationUIViewController
You have to subclass both of your view controllers, the parent view controller and the modal view controller it presents (somebody correct me if i'm wrong, but child controllers only handle rotation if their parent also handles them) and override the method called:
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
You should return YES for both orientations (portrait and landscape). By default, view controllers on the iphone only return YES for UIInterfaceOrientationPortrait
, so you have to handle rotation to landscape yourself.
Read up on this UIViewController Class Reference and this Technical Q&A QA1688: Why won't my UIViewController rotate with the device?.
精彩评论