在Swift中解雇GameCenter视图

时间:2014-10-29 20:03:12

标签: swift game-center

显示游戏中心视图后,点击完成后不会解除。这是我的代码来展示它:

let gameCenterController = GKGameCenterViewController()

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

我错过了什么?

1 个答案:

答案 0 :(得分:0)

在Ray Wenderlich,有一些很好的Q& A。请参阅下面代码中的最后一个方法。来源:

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

}