KeyboardObserver
用于更简单的键盘事件处理。
特性
- 更简单的键盘事件处理。
- 不使用
Notification
,而是使用event
。
区别
不使用KeyboardObserver.swift
let keyboardNotifications: [Notification.Name] = [
.UIKeyboardWillShow,
.UIKeyboardWillHide,
.UIKeyboardWillChangeFrame,
]
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
keyboardNotifications.forEach {
NotificationCenter.default.addObserver(self, selector: #selector(keyboardEventNotified:), name: $0, object: nil)
}
}
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
keyboardNotifications.forEach {
NotificationCenter.default.removeObserver(self, name: $0, object: nil)
}
}
@objc func keyboardEventNotified(notification: NSNotification) {
guard let userInfo = notification.userInfo else { return }
let keyboardFrameEnd = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let curve = UIView.AnimationCurve(rawValue: (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as! NSNumber).intValue)!
let options = UIView.AnimationOptions(rawValue: UInt(curve.rawValue << 16))
let duration = TimeInterval(truncating: userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as! NSNumber)
let bottom = keyboardFrameEnd.height - bottomLayoutGuide.length
UIView.animate(withDuration: duration, delay: 0.0, options: [options], animations:
{ () -> Void in
self.textView.contentInset.bottom = bottom
self.textView.scrollIndicatorInsets.bottom = bottom
} , completion: nil)
}
使用KeyboardObserver
let keyboard = KeyboardObserver()
override func viewDidLoad() {
super.viewDidLoad()
keyboard.observe { [weak self] (event) -> Void in
guard let self = self else { return }
switch event.type {
case .willShow, .willHide, .willChangeFrame:
print("Fire: \(event.type)")
let keyboardFrameEnd = event.keyboardFrameEnd
let bottom = keyboardFrameEnd.height - self.bottomLayoutGuide.length
UIView.animate(withDuration: event.duration, delay: 0.0, options: [event.options], animations: { () -> Void in
self.textView.contentInset.bottom = bottom
self.textView.scrollIndicatorInsets.bottom = bottom
}, completion: nil)
default:
break
}
}
}
使用方法
在您希望的位置创建KeyboardObserver
实例,该实例会监视键盘直到deinit。
调用observe(event: KeyboardEvent)
来监视键盘事件。event
是转换键盘通知对象的。
public struct KeyboardEvent {
public let type: KeyboardEventType
public let keyboardFrameBegin: CGRect
public let keyboardFrameEnd: CGRect
public let curve: UIViewAnimationCurve
public let duration: NSTimeInterval
public var isLocal: Bool?
public var options: UIViewAnimationOptions {
return UIViewAnimationOptions(rawValue: UInt(curve.rawValue << 16))
}
...
event
具有上述属性。您不需要将Notification
的userInfo转换为提取键盘事件值。
KeyboardEentType
有与键盘通知名称相同的类型。如下所示
public enum KeyboardEventType {
case willShow
case didShow
case willHide
case didHide
case willChangeFrame
case didChangeFrame
...
}
它也有一个public var notificationName: String
,您可以通过它获取原始通知名称。
运行时要求
- iOS 8.0 或更高版本
- Xcode 10.0
- Swift 4.2
安装和配置
信息:要使用针对低于 iOS 8.0 的项目,必须直接在项目中包含KeyboardObserver.swift
源文件。
使用 Carthage 安装
只需将其添加到 Cartfile 中
github "morizotter/KeyboardObserver"
使用 CocoaPods 安装
CocoaPods 是一个集成的依赖管理器,它自动化向 Cocoa 应用程序中添加库的过程。您可以使用以下命令安装它:
$ gem update
$ gem install cocoapods
$ pods --version
要使用 CocoaPods 将 KeyboardObserver 集成到 Xcode 项目中,请在 Podfile
中指定它,并运行 pod install
。
platform :ios, '8.0'
use_frameworks!
pod "KeyboardObserver", '~>2.0.0'
手动安装
要在没有依赖管理器的的情况下安装 KeyboardObserver,请在 Xcode 项目中添加 KeyboardObserver.swift
。
贡献
请提交您想看到的任何问题或拉取请求!我们正在等待! :)
许可
KeyboardObserver.swift是在MIT许可证下发布的。请阅读LICENSE文件以获取更多信息。