gpt4 book ai didi

swift - 如何在ios9中检测两次点击主页按钮

转载 作者:搜寻专家 更新时间:2023-10-31 22:59:27 25 4
gpt4 key购买 nike

在我正在编写以学习 swift 和 iOS9 的应用程序中,当用户双击主页按钮并进入应用程序切换器时,我试图暂停我的 NStimer,根据 ios9 matt neuberg 的编程,当用户双击主页按钮时,用户现在可以在应用程序切换器界面中工作。如果您的应用程序位于最前面,您的应用程序代表会收到此消息:applicationWillResignActive:

但我的计时器仅在我点击主页按钮一次和点击两次并使用应用程序切换器时暂停,我看到我的计时器正在计时,有什么想法吗?

最佳答案

尝试在您的 AppDelegate.swift 中添加以下行:

static let kAppDidBecomeActive = "kAppDidBecomeActive"
static let kAppWillResignActive = "kAppWillResignActive"

func applicationDidBecomeActive(application: UIApplication) {
// Your application is now the active one
// Take into account that this method will be called when your application is launched and your timer may not initialized yet
NSNotificationCenter.defaultCenter().postNotificationName("kAppDidBecomeActive", object: nil)
}

func applicationWillResignActive(application: UIApplication) {
// Home button is pressed twice
NSNotificationCenter.defaultCenter().postNotificationName("kAppWillResignActive", object: nil)
}

此外,将您的 View Controller 设置为这些通知的观察者:

override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(pauseGame), name: AppDelegate.kAppWillResignActive, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(resumeGame), name: AppDelegate.AppDelegate.kAppDidBecomeActive, object: nil)

}

关于swift - 如何在ios9中检测两次点击主页按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38996610/

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