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

时间:2011-07-20 19:39:12

标签: uiviewcontroller cocos2d-iphone orientation game-center

我使用下面的代码显示游戏中心,但如果我旋转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 );
        }

还有其他方法可以展示吗?任何帮助PLZ

谢谢

2 个答案:

答案 0 :(得分:1)

呵呵我发现可能是一个特定的解决方案,所以我用以下代码替换了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];
    }

还需要为viewController添加一个属性到AppDelegate.h首先添加一个属性声明,如下所示:

@property (nonatomic, assign) RootViewController *viewController;

然后切换到AppDelegate.m并合成变量,如下所示:

@synthesize viewController;

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

#define GAME_AUTOROTATION kGameAutorotationUIViewController

答案 1 :(得分:0)

你必须子类化你的两个视图控制器,父视图控制器和它提供的模态视图控制器(有人纠正我,如果我错了,但子控制器只处理轮换,如果他们的父也处理它们)并覆盖方法叫做:

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

对于两个方向(纵向和横向),您应该返回YES。默认情况下,iphone上的视图控制器仅为UIInterfaceOrientationPortrait返回YES,因此您必须自己处理旋转以进行横向渲染。

阅读此UIViewController Class Reference和此Technical Q&A QA1688: Why won't my UIViewController rotate with the device?