在您的课程中包含GKGameCenterControllerDelegate
协议。
class ViewController: UIViewController, GKGameCenterControllerDelegate
当点击“完成”时,此方法会关闭 Game Center 视图:
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!) {
self.dismissViewControllerAnimated(true, completion: nil)
}
此函数包含显示排行榜所需的代码:
func showLeaderboard() {
// declare the Game Center viewController
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"
// Finally present the Game Center ViewController
self.showViewController(gcViewController, sender: self)
self.navigationController?.pushViewController(gcViewController, animated: true)
self.presentViewController(gcViewController, animated: true, completion: nil)
}
您现在可以showLeaderboard
通过按 a来触发该功能UIButton
:
@IBAction func buttonShowLeaderboard(sender: AnyObject) {
showLeaderboard()
}