KeyboardObserver是为了处理不太复杂的键盘事件。
特性:处理不太复杂的键盘事件。
不是使用NSNotificatio,而是使用evet。
区别:不用KeyboardObserver.swift
let keyboardNotificatios = [ UIKeyboardWillShowNotificatio, UIKeyboardWillHideNotificatio, UIKeyboardWillChageFrameNotificatio]override fuc viewDidLoad() { super.viewDidLoad()}override fuc viewWillAppear(aimated: Bool) { super.viewWillAppear(aimated) keyboardNotificatios.forEach { NSNotificatioCeter.defaultCeter().addObserver(self, selector: "keyboardEvetNotified:", ame: $0, object: il) }}override fuc viewWillDisappear(aimated: Bool) { super.viewWillDisappear(aimated) keyboardNotificatios.forEach { NSNotificatioCeter.defaultCeter().removeObserver(self, ame: $0, object: il) }}fuc keyboardEvetNotified(otificatio: NSNotificatio) { guard let userIfo = otificatio.userIfo else { retur } let keyboardFrameEd = (userIfo[UIKeyboardFrameEdUserIfoKey] as! NSValue).CGRectValue() let curve = UIViewAimatioOptios(rawValue: UIt(userIfo[UIKeyboardAimatioCurveUserIfoKey] as! NSNumber)) let duratio = NSTimeIterval(userIfo[UIKeyboardAimatioDuratioUserIfoKey] as! NSNumber) let distace = UIScree.maiScree().bouds.height - keyboardFrameEd.origi.y let bottom = distace >= bottomLayoutGuide.legth ? distace : bottomLayoutGuide.legth UIView.aimateWithDuratio(duratio, delay: 0.0, optios: [curve], aimatios: { [weak self] () -> Void i self?.textView.cotetIset.bottom = bottom self?.textView.scrollIdicatorIsets.bottom = bottom } , completio: il)}用KeyboardObserver.swift
let keyboard = KeyboardObserver()override fuc viewDidLoad() { super.viewDidLoad() keyboard.observe { [weak self] (evet) -> Void i guard let s = self else { retur } switch evet.type { case .WillShow, .WillHide, .WillChageFrame: let distace = UIScree.maiScree().bouds.height - evet.keyboardFrameEd.origi.y let bottom = distace >= s.bottomLayoutGuide.legth ? distace : s.bottomLayoutGuide.legth UIView.aimateWithDuratio(evet.duratio, delay: 0.0, optios: [evet.curve], aimatios: { [weak self] () -> Void i self?.textView.cotetIset.bottom = bottom self?.textView.scrollIdicatorIsets.bottom = bottom } , completio: il) default: break } }}
评论