【问题标题】:how to detect tap home-button twice in ios9如何在ios9中检测两次点击主页按钮
【发布时间】:2016-12-24 02:38:39
【问题描述】:

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

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

【问题讨论】:

  • 显示applicationWillResignActive 中的代码
  • 我在 applicationWillResi‌​gnActive 中包含代码,我只是在我的选择器操作中使用 nsnotification,如下所示 override func viewDidLoad() { super.viewDidLoad() // 当主页按钮点击两次时暂停游戏。 NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.pauseGame), name: UIApplicationWillResignActiveNotification, object: nil) } 在暂停游戏函数中,是调用者 timer.invalidate()

标签: swift ios9 uiapplicationdelegate home-button


【解决方案1】:

尝试在您的 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)
} 

此外,将您的视图控制器设置为这些通知的观察者:

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)

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-13
    • 2011-02-24
    • 2023-02-25
    • 1970-01-01
    • 2017-05-23
    • 2012-02-11
    • 1970-01-01
    • 2016-05-31
    相关资源
    最近更新 更多