ApplicationEventObserver
Swift中对应用事件通知(例如UIApplicationDidBecomeActiveNotification)的处理。
🎉 特性
- 您不需要使用
NSNotificationCenter。 - 您只能捕捉您想要的的事件。
- 您可以在任何时候进行
subscribe/resume/suspend/dispose操作。
✏️ 如何使用
首先,您创建一个ApplicationEventObserver实例。
该实例会 observing UIApplication事件直到其被释放(deinit)。
(例如)
class ViewController: UIViewController {
private lazy var appEventObserver = ApplicationEventObserver()
...
}其次,订阅您想要捕捉的事件。
例如,若要捕获UIApplicationDidBecomeActive事件,您可以在handler(closure)中检查返回值event的类型。
func viewDidLoad() {
super.viewDidLoad()
appEventObserver.subscribe() { event in
switch event.type {
case .DidBecomeActive:
print(event.type.notificationName)
default:
break
}
}
}您也可以随时暂停/恢复。
如果实例的状态是被暂停的,则停止观察所有事件,直到调用resume()
(例如)
func showPopup() {
appEventObserver.suspend()
}
...
func closePopup() {
appEventObserver.resume()
}Specs
ApplicationEvent (结构体)
ApplicationEvent有两个参数。
- type :
ApplicationEventType - value : 如果实例存在,则为
NSNotification实例的userInfo值。作为AnyObject?提供。
📌 示例
之前(使用NSNotificationCenter)
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "notified:",
name: UIApplicationDidBecomeActiveNotification,
object: nil
)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: "notified:",
name: UIApplicationWillChangeStatusBarFrameNotification,
object: nil
)
}
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}
}
private extension ViewController {
@objc private func notified(notification: NSNotification) {
switch notification.name {
case UIApplicationDidBecomeActiveNotification:
print(notification.name)
case UIApplicationWillChangeStatusBarFrameNotification:
let rect = notification.userInfo?[UIApplicationStatusBarFrameUserInfoKey]
print(notification.name,rect)
default:
break
}
}
}之后(使用ApplicationEventObserver)
import UIKit
import ApplicationEventObserver
class ViewController: UIViewController {
private lazy var appEventObserver = ApplicationEventObserver()
override func viewDidLoad() {
super.viewDidLoad()
appEventObserver.subscribe() { event in
switch event.type {
case .DidBecomeActive, .WillResignActive:
print(event.type.notificationName)
case .WillChangeStatusBarFrame:
if let v = event.value {
print(event.type.notificationName, v)
}
default:
break
}
}
}
}如此简单!!
支持的事件
- 以下是支持的事件列表
| 通知名称 | ApplicationEventType |
|---|---|
| UIApplicationDidFinishLaunchingNotification | .DidFinishLaunching |
| UIApplicationWillEnterForegroundNotification | .WillEnterForeground |
| UIApplicationDidEnterBackgroundNotification | .DidEnterBackground, |
| UIApplicationWillResignActiveNotification | .WillResignActive, |
| UIApplicationDidBecomeActiveNotification | .DidBecomeActive, |
| UIApplicationDidReceiveMemoryWarningNotification | .DidReceiveMemoryWarning, |
| UIApplicationWillTerminateNotification | .WillTerminate, |
| UIApplicationSignificantTimeChangeNotification | .SignificantTimeChange, |
| UIApplicationWillChangeStatusBarOrientationNotification | .WillChangeStatusBarOrientation, |
| UIApplicationDidChangeStatusBarOrientationNotification | .DidChangeStatusBarOrientation, |
| UIApplicationWillChangeStatusBarFrameNotification | .WillChangeStatusBarFrame |
| UIApplicationDidChangeStatusBarFrameNotification | .DidChangeStatusBarFrame |
| UIApplicationBackgroundRefreshStatusDidChangeNotification | .BackgroundRefreshStatusDidChange |
选项
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
→ 可以获取方向值。
UIApplicationWillChangeStatusBarOrientationNotification
UIApplicationDidChangeStatusBarOrientationNotification
→ 可以获取rect值。
要求
- iOS 8.0+
- Xcode 7.0+(Swift 2+)
安装和设置
使用 Carthage
- 只需将以下行添加到您的 Cartfile
github "sgr-ksmt/ApplicationEventObserver"- 在终端上运行
carthage update - 按描述添加框架。详情:Carthage README
使用 CocoaPods
只需将以下行添加到您的 Podfile
pod 'ApplicationEventObserver'- 在终端上运行
pod install