如何让UIViewcontroller(游戏中心)响应方向(横向或纵向)?
我使用下面的代码来显示游戏中心,但如果我旋转 iPhone,什么也不会发生,排行榜将颠倒(不响应方向)
代码
// 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
leaderboardController.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 );
}
还有其他方法可以显示它吗?有什么帮助请
谢谢
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
leaderboardController.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
呵呵,我可能找到了一个特定的解决方案,因此我用下面的代码替换了
if (leaderboardController != nil){...}
。代码:
还需要在
AppDelegate.h
中添加viewController的属性,首先添加属性声明,如下所示:然后切换到
AppDelegate.m
并合成 最后,由于此代码使用 UIKit 来显示视图控制器,因此最好将 Cocos2D 设置为使用 UIViewController 旋转。
于是进入GameConfig.h,注释掉当前的
#define GAME_AUTOROTATION kGameAutorotationNone
,进行设置,如图:huh I found maybe a specific solution so I replace the
if (leaderboardController != nil){...}
by the code below.The code :
also need to add a property for the viewController to the
AppDelegate.h
First add a property declaration, as shown below:Then switch to
AppDelegate.m
and synthesize the variable, as shown down: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:您必须对视图控制器、父视图控制器及其呈现的模态视图控制器进行子类化(如果我错了,有人纠正我,但子控制器仅在其父控制器也处理它们时才处理旋转)并覆盖调用的方法:
对于两个方向(纵向和横向),您都应该返回 YES。默认情况下,iPhone 上的视图控制器仅对
UIInterfaceOrientationPortrait
返回 YES,因此您必须自行处理旋转以实现横向显示。阅读UIViewController 类参考 和这个技术问答QA1688:
为什么我的 UIViewController 不随设备旋转?。
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:
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?.