NoticeObserveKit
NoticeObserveKit 是一个类型安全的 NotificationCenter 包装器。
// .keyboardWillShow is a static property.
Notice.Center.default.observe(name: .keyboardWillShow) { keyboardInfo in
// In this case, keyboardInfo is UIKeyboardInfo type.
// It is inferred from a generic parameter of Notice.Name<Value>.
print(keyboardInfo)
}
// pool is Notice.ObserverPool.
// If pool is released, Notice.Observes are automatically removed.
.invalidated(by: pool)
用法
首先,您需要像这样实现 Notice.Name<T>
。 T
是 notification.userInfo 中值的类型。
extension Notice.Names {
static let keyboardWillShow = Notice.Name<UIKeyboardInfo>(UIResponder.keyboardWillShowNotification)
}
如果您定义自定义对象,您需要使用 NoticeUserInfoDecodable
协议实现它。为了确认此协议,您必须实现 init?(info: [AnyHashable : Any])
和 func dictionaryRepresentation() -> [AnyHashable : Any]
。
struct UIKeyboardInfo: NoticeUserInfoDecodable {
let frame: CGRect
let animationDuration: TimeInterval
let animationCurve: UIViewAnimationOptions
init?(info: [AnyHashable : Any]) {
guard
let frame = (info[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration = info[UIKeyboardAnimationDurationUserInfoKey] as? TimeInterval,
let curve = info[UIKeyboardAnimationCurveUserInfoKey] as? UInt
else {
return nil
}
self.frame = frame
self.animationDuration = duration
self.animationCurve = UIViewAnimationOptions(rawValue: curve)
}
}
对于小于 v0.4.0 的用法,请参阅 documents/v0_4_0。
自定义
如果您可以像这样发布自定义 Notification。
extension Notice.Names {
static let navigationControllerDidShow = Notice.Name<NavigationControllerContent>(name: "navigationControllerDidShow")
}
let content = NavigationControllerContent(viewController: viewController, animated: animated)
Notice.Center.default.post(name: .navigationControllerDidShow, value: content)
您可以这样手动无效化。
let observer = Notice.Center.default.observe(name: .keyboardWillShow) { keyboardInfo in
print(keyboardInfo)
}
observer.invalidate()
您可以使用 vi 通知中心。
NotificationCenter.default.nok.observe(name: .keyboardWillShow) { keyboardInfo in
print(keyboardInfo)
}
.invalidated(by: pool)
示例
import UIKit
import NoticeObserveKit
class ViewController: UIViewController {
private let searchBar = UISearchBar(frame: .zero)
private var pool = Notice.ObserverPool()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
navigationItem.titleView = searchBar
configureObservers()
}
private func configureObservers() {
Notice.Center.default.observe(name: .keyboardWillShow) {
print("UIKeyboard will show = \($0)")
}.invalidated(by: pool)
Notice.Center.default.observe(name: .keyboardWillHide) {
print("UIKeyboard will hide = \($0)")
}.invalidated(by: pool)
}
}
需求
- Swift 5
- Xcode 10.2或更高版本
- iOS 10.0或更高版本
- tvOS 10.0或更高版本
- macOS 10.10或更高版本
- watchOS 3.0或更高版本
安装
CocoaPods
NoticeObserveKit可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile:
pod "NoticeObserveKit"
Carthage
如果您使用Carthage,只需将NoticeObserveKit添加到您的Cartfile
github "marty-suzuki/NoticeObserveKit"
确保将NoticeObserveKit.framework
添加到“链接框架和库”和“复制框架”构建阶段。
作者
marty-suzuki,[email protected]
许可证
NoticeObserveKit可供个人或商业用途,根据MIT许可证进行。关于更多信息,请参阅LICENSE文件。