如何让UIViewcontroller(游戏中心)响应方向(横向或纵向)?

发布于 2024-11-25 08:07:45 字数 1673 浏览 1 评论 0原文

我使用下面的代码来显示游戏中心,但如果我旋转 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖树树初阳… 2024-12-02 08:07:45

呵呵,我可能找到了一个特定的解决方案,因此我用下面的代码替换了 if (leaderboardController != nil){...}

代码:

if (leaderboardController != NULL) {
        leaderboardController.category = category; 

        leaderboardController.timeScope =GKLeaderboardTimeScopeAllTime;

        leaderboardController.leaderboardDelegate = self; 
        AppDelegate *delegate = [UIApplication sharedApplication].delegate; 

        [delegate.viewController presentModalViewController:leaderboardController animated:YES];
    }

还需要在AppDelegate.h中添加viewController的属性,首先添加属性声明,如下所示:

@property (nonatomic, assign) RootViewController *viewController;

然后切换到AppDelegate.m并合成 最后,

@synthesize viewController;

由于此代码使用 UIKit 来显示视图控制器,因此最好将 Cocos2D 设置为使用 UIViewController 旋转。
于是进入GameConfig.h,注释掉当前的#define GAME_AUTOROTATION kGameAutorotationNone,进行设置,如图:

#define GAME_AUTOROTATION kGameAutorotationUIViewController

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
绮烟 2024-12-02 08:07:45

您必须对视图控制器、父视图控制器及其呈现的模态视图控制器进行子类化(如果我错了,有人纠正我,但子控制器仅在其父控制器也处理它们时才处理旋转)并覆盖调用的方法:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

对于两个方向(纵向和横向),您都应该返回 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:

-(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?
.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文