2017-06-02 22 views
0

當用戶點擊statusBar時,是否有任何通知?我想在用戶點擊statusBar時專門做一些事情,所以我需要知道事件發生的時間。 。我們可以監視statusBar點擊事件嗎?

+0

看到這個https://stackoverflow.com/questions/3753097/how-to-detect-touches-in-status-bar –

+0

的可能的複製[如何檢測在狀態欄觸摸(HTTPS:/ /stackoverflow.com/questions/3753097/how-to-detect-touches-in-status-bar) – dengApro

回答

0

Yes.By這個被竊聽solution.not點擊solution.because狀態欄的方式不是像元素按鈕:)

這裏是SWIFT版本3:

中的appdelegate功能

加入這一行。

let statusBarTappedNotification = Notification(name: Notification.Name(rawValue: "statusBarTappedNotification")) 

然後添加此功能。

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    super.touchesBegan(touches, with: event) 

    let statusBarRect = UIApplication.shared.statusBarFrame 
    guard let touchPoint = event?.allTouches?.first?.location(in: self.window) else { return } 

    if statusBarRect.contains(touchPoint) { 
     NotificationCenter.default.post(statusBarTappedNotification) 
    } 
} 

現在它已經準備好在您的視圖控制器中使用。

NotificationCenter.default.addObserver(forName: statusBarTappedNotification.name, object: .none, queue: .none) { _ in 
    print("Viola, i have been tapped!!!!") 
} 
相關問題