KeyboardSpy 1.1

KeyboardSpy 1.1

测试已测试
Lang语言 SwiftSwift
许可证 MIT
发布上次发布2017年9月
SwiftSwift 版本3.0
SPM支持 SPM

Dalton Hinterscher 维护。



  • 作者
  • Daltron

KeyboardSpy




使用 Swift 4 编写

KeyboardSpy 是一个超级轻量级且易于使用的包装器,使得在 iOS 中观察键盘通知变得轻而易举。

需求

  • iOS 8.0+
  • xCode 9.0+

使用方法

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 文件。