测试已测试 | ✗ |
Lang语言 | SwiftSwift |
许可证 | MIT |
发布上次发布 | 2017年9月 |
SwiftSwift 版本 | 3.0 |
SPM支持 SPM | ✗ |
由 Dalton Hinterscher 维护。
KeyboardSpy 是一个超级轻量级且易于使用的包装器,使得在 iOS 中观察键盘通知变得轻而易举。
KeyboardSpy 使用基于协议的方法来观察键盘通知
public protocol KeyboardSpyAgent {
var keyboardEventsToSpyOn: [KeyboardSpyEvent] { get }
func keyboardSpyEventProcessed(event:KeyboardSpyEvent, keyboardInfo: KeyboardSpyInfo)
}
要添加一个间谍,只需
KeyboardSpy.spy(on: self)
要移除一个间谍,只需
KeyboardSpy.unspy(on: self)
您可以观察 六种不同的事件
public enum KeyboardSpyEvent {
case willShow
case didShow
case willHide
case didHide
case willChangeFrame
case didChangeFrame
}
对于每一个您所监视的事件,您将得到以下对象
public class KeyboardSpyInfo: NSObject {
public private(set) var beginFrame: CGRect!
public private(set) var endFrame: CGRect!
public private(set) var animationCurve: UIViewAnimationCurve!
public private(set) var animationDuration: Double!
public private(set) var isLocal: Bool!
public var keyboardHeight: CGFloat
}
import KeyboardSpy
class KeyboardSpyViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
KeyboardSpy.spy(on: keyboardSpyView) // This can be placed anywhere
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
KeyboardSpy.unspy(on: keyboardSpyView) // This can be placed anywhere
}
}
extension KeyboardSpyViewController: KeyboardSpyAgent {
internal var keyboardEventsToSpyOn: [KeyboardSpyEvent] {
return [.willShow, .willHide]
}
internal func keyboardSpyEventProcessed(event: KeyboardSpyEvent, keyboardInfo: KeyboardSpyInfo) {
if event == .willShow {
// Do something like moving a view above the keyboard
} else if event == .willHide {
// Do something like moving a view back to its original position
}
}
}
Dalton Hinterscher, [email protected]
KeyboardSpy 采用 MIT 许可证。有关更多信息,请参阅 LICENSE 文件。