gpt4 book ai didi

swift - 在 Swift 中关闭 GameCenter View

转载 作者:行者123 更新时间:2023-11-28 09:18:02 26 4
gpt4 key购买 nike

显示 Game Center View 后,它不会在点击完成后关闭。这是我的代码来展示它:

let gameCenterController = GKGameCenterViewController()

self.presentViewController(gameCenterController, animated:true, completion: nil)

我错过了什么?

最佳答案

Ray Wenderlich 对此有一些很好的问答。请参阅下面代码中的最后一个方法。来源:

http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=18671

class GameViewController: UIViewController, GKGameCenterControllerDelegate {

var skView: SKView!
var scene: GameScene!

override func viewDidLoad() {
super.viewDidLoad()

// View
//------
skView = self.view as SKView
skView.ignoresSiblingOrder = true

scene = GameScene.sceneWithSize(skView.bounds.size)
scene.scaleMode = .AspectFill
scene.view?.window?.rootViewController = self
skView.presentScene(scene)

authenticateLocalPlayer()
}


override func touchesEnded(touches: NSSet, withEvent event: UIEvent) {
var touch:UITouch = touches.anyObject() as UITouch
var location:CGPoint = touch.locationInNode(scene)

if (scene.gameCenterRect.contains(location) && GKLocalPlayer.localPlayer().authenticated) {
self.openGameCenter()
}
}


func openGameCenter() {
var gameCenter = GKGameCenterViewController()
gameCenter.gameCenterDelegate = self
self.presentViewController(gameCenter, animated: true, completion: nil)
}


func authenticateLocalPlayer(){
var localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController, animated: true, completion: nil)
}else{
println("(GameCenter) Player authenticated: \(GKLocalPlayer.localPlayer().authenticated)")
}

}

}


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

[...] // standard methods

}

关于swift - 在 Swift 中关闭 GameCenter View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639573/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com