KeyboardObserver

程序名称:KeyboardObserver

授权协议: MIT

操作系统: iOS

开发语言: Swift

KeyboardObserver 介绍

KeyboardObserver 是为了处理不太复杂的键盘事件。

  • 处理不太复杂的键盘事件。

  • 不是使用NSNotification,而是使用 event。

不用 KeyboardObserver.swift

let keyboardNotifications = [
    UIKeyboardWillShowNotification,
    UIKeyboardWillHideNotification,
    UIKeyboardWillChangeFrameNotification
]
override func viewDidLoad() {    super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {    super.viewWillAppear(animated)

    keyboardNotifications.forEach {
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardEventNotified:", name: $0, object: nil)
    }
}
override func viewWillDisappear(animated: Bool) {    super.viewWillDisappear(animated)

    keyboardNotifications.forEach {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: $0, object: nil)
    }
}func keyboardEventNotified(notification: NSNotification) {    guard let userInfo = notification.userInfo else { return }    let keyboardFrameEnd = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()    let curve = UIViewAnimationOptions(rawValue: UInt(userInfo[UIKeyboardAnimationCurveUserInfoKey] as! NSNumber))    let duration = NSTimeInterval(userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber)    let distance = UIScreen.mainScreen().bounds.height - keyboardFrameEnd.origin.y    let bottom = distance >= bottomLayoutGuide.length ? distance : bottomLayoutGuide.length

    UIView.animateWithDuration(duration, delay: 0.0, options: [curve], animations:
        { [weak self] () -> Void in
            self?.textView.contentInset.bottom = bottom            self?.textView.scrollIndicatorInsets.bottom = bottom
        } , completion: nil)
}

用 KeyboardObserver.swift

let keyboard = KeyboardObserver()override func viewDidLoad() {    super.viewDidLoad()

    keyboard.observe { [weak self] (event) -> Void in
        guard let s = self else { return }        switch event.type {        case .WillShow, .WillHide, .WillChangeFrame:            let distance = UIScreen.mainScreen().bounds.height - event.keyboardFrameEnd.origin.y            let bottom = distance >= s.bottomLayoutGuide.length ? distance : s.bottomLayoutGuide.length

            UIView.animateWithDuration(event.duration, delay: 0.0, options: [event.curve], animations:
                { [weak self] () -> Void in
                    self?.textView.contentInset.bottom = bottom                    self?.textView.scrollIndicatorInsets.bottom = bottom
                } , completion: nil)        default:            break
        }
    }
}

KeyboardObserver 官网

https://github.com/morizotter/KeyboardObserver

相关编程语言

Acapela TTS 是一个为 iPhone 和 iPad 开发的 TTS 引...
二维码(QR Code)扫描静态库,扫描效率较高。
RegexKitLite 是一个轻量级的 Objective-C 的正则表...
一款基于ASIHttpReques开源的仿迅雷多线程断点续传功...
实现动态检测网络(wifi)状况,不需要用户手动刷新...
使用iphoneSDK官方NSXMLParserDelegate做的简单xml解...