延迟GameCenter身份验证视图控制器的演示-Swift 4.2

时间:2019-02-26 22:22:08

标签: authentication game-center viewdidload swift4.2

我正在尝试将GameCenter Authentication View Controller 延迟2秒,然后再显示。我的VC中大约有2秒钟的按钮动画,我不希望它们被GameCenter Authentication View Controller 的显示所阻塞。有什么建议吗?

override func viewDidLoad() {
    super.viewDidLoad()

     // Game Center - Authentication of player
     NotificationCenter.default.addObserver(self, selector: #selector(showAuthenticationViewController), name: NSNotification.Name(GameKitHelper.PresentAuthenticationViewController), object: nil)

     GameKitHelper.sharedInstance.authenticateLocalPlayer()
}

// Game Center - Presents authentication view controller
@objc func showAuthenticationViewController() {
    let gameKitHelper = GameKitHelper.sharedInstance
    if let authenticationViewController =
        gameKitHelper.authenticationViewController {
        self.present(authenticationViewController, animated: true, completion: nil)
    }

}

// add this to deregister for notifications when the object is deallocated
deinit {
    NotificationCenter.default.removeObserver(self)
}

1 个答案:

答案 0 :(得分:2)

也许,好的旧版大型中央调度(GCD)可以完成工作。 2.0持续2秒,但是将其更改为所需的时间。

override func viewDidLoad() {
    super.viewDidLoad()

    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {

         // Game Center - Authentication of player
        NotificationCenter.default.addObserver(self, selector: #selector(self.showAuthenticationViewController), name: NSNotification.Name(GameKitHelper.PresentAuthenticationViewController), object: nil)

        GameKitHelper.sharedInstance.authenticateLocalPlayer()
    }
}