Swift 显示 Game Center 排行榜

Swift Displaying Game Center Leaderboards

现在我正在尝试显示我创建的排行榜。玩家认证正常,但是打开游戏中心window就很奇怪了。这是一张图片:

这是我用来显示这张图片的代码:

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        self.showLeaderboard()
    }
    func showLeaderboard() {
        var leaderView = UIViewController()

       var leaderViewController = GKGameCenterViewController(rootViewController: leaderView)
         leaderViewController.viewState = GKGameCenterViewControllerState.Leaderboards
       leaderViewController.leaderboardIdentifier = "High_Score_Board"
       self.showViewController(leaderViewController, sender: self)
        //self.presentViewController(leaderViewController, animated: true, completion: nil)

    }
    func leaderboardViewControllerDidFinish(controller: GKGameCenterViewController){
        controller.dismissViewControllerAnimated(true, completion: nil)
    }

所有这些都在我的 GameViewController 中。此外,即使这可行,我将如何在我的 SKScenes 中访问此方法?感谢您的帮助!

导入 GameKit:

import GameKit

确保在 class.

中添加 GKGameCenterControllerDelegate 代表
class ViewController: UIViewController, GKGameCenterControllerDelegate {
    ...
}

该委托需要一个方法,当玩家点击 "Done" 按钮时调用该方法。

func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
    self.dismissViewControllerAnimated(true, completion: nil)
}

此函数包含显示排行榜所需的代码:

func showLeaderboard() {        
    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self
    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    // Remember to replace "Best Score" with your Leaderboard ID (which you have created in iTunes Connect)
    gcViewController.leaderboardIdentifier = "Best_Score"
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
     // self.presentViewController(gcViewController, animated: true, completion: nil)
}

您现在可以通过按一个按钮来呼叫 showLeaderboard

@IBAction func buttonShowLeaderboard(sender: AnyObject) {
    showLeaderboard()
}