|
9 | 9 | import UIKit |
10 | 10 |
|
11 | 11 | open class KeyboardAvoiding { |
12 | | - |
13 | | - open var showAnimationBlock: ((CGRect) -> Void)? |
14 | | - open var hideAnimationBlock: (() -> Void)? |
15 | | - |
16 | | - public init(_ showAnimationBlock: ((CGRect) -> Void)? = nil, |
17 | | - _ hideAnimationBlock: (() -> Void)? = nil) { |
18 | | - |
19 | | - self.showAnimationBlock = showAnimationBlock |
20 | | - self.hideAnimationBlock = hideAnimationBlock |
| 12 | + |
| 13 | + open var willShow: ((CGRect) -> Void)? |
| 14 | + open var didShow: ((CGRect) -> Void)? |
| 15 | + open var willHide: (() -> Void)? |
| 16 | + open var didHide: (() -> Void)? |
| 17 | + |
| 18 | + public init(willShow: ((CGRect) -> Void)? = nil, |
| 19 | + didShow: ((CGRect) -> Void)? = nil, |
| 20 | + willHide: (() -> Void)? = nil, |
| 21 | + didHide: (() -> Void)? = nil) { |
| 22 | + |
| 23 | + self.willShow = willShow |
| 24 | + self.didShow = didShow |
| 25 | + self.willHide = willHide |
| 26 | + self.didHide = didHide |
21 | 27 | } |
22 | | - |
23 | | - open func start() { |
| 28 | + |
| 29 | + public func onKeyboardWillShow(_ willShow: @escaping ((CGRect) -> Void)) -> KeyboardAvoiding { |
| 30 | + self.willShow = willShow |
| 31 | + return self |
| 32 | + } |
| 33 | + |
| 34 | + public func onKeyboardDidShow(_ didShow: @escaping ((CGRect) -> Void)) -> KeyboardAvoiding { |
| 35 | + self.didShow = didShow |
| 36 | + return self |
| 37 | + } |
| 38 | + |
| 39 | + public func onKeyboardWillHide(_ willHide: @escaping (() -> Void)) -> KeyboardAvoiding { |
| 40 | + self.willHide = willHide |
| 41 | + return self |
| 42 | + } |
| 43 | + |
| 44 | + public func onKeyboardDidHide(_ didHide: @escaping (() -> Void)) -> KeyboardAvoiding { |
| 45 | + self.didHide = didHide |
| 46 | + return self |
| 47 | + } |
| 48 | + |
| 49 | + public func start() { |
24 | 50 | NotificationCenter.default.addObserver(self, |
25 | | - selector: #selector(self.onKeyboardShow(_:)), |
| 51 | + selector: #selector(self.keyboardWillShow(_:)), |
26 | 52 | name: UIResponder.keyboardWillShowNotification, |
27 | 53 | object: nil) |
28 | 54 |
|
29 | 55 | NotificationCenter.default.addObserver(self, |
30 | | - selector: #selector(self.onKeyboardHide(_:)), |
| 56 | + selector: #selector(self.keyboardWillHide(_:)), |
31 | 57 | name: UIResponder.keyboardWillHideNotification, |
32 | 58 | object: nil) |
| 59 | + |
| 60 | + NotificationCenter.default.addObserver(self, |
| 61 | + selector: #selector(self.keyboardDidShow(_:)), |
| 62 | + name: UIResponder.keyboardDidShowNotification, |
| 63 | + object: nil) |
| 64 | + |
| 65 | + NotificationCenter.default.addObserver(self, |
| 66 | + selector: #selector(self.keyboardDidHide(_:)), |
| 67 | + name: UIResponder.keyboardDidHideNotification, |
| 68 | + object: nil) |
33 | 69 | } |
34 | 70 |
|
35 | | - open func stop() { |
| 71 | + public func stop() { |
36 | 72 | NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillShowNotification, object: nil) |
| 73 | + NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidShowNotification, object: nil) |
37 | 74 | NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardWillHideNotification, object: nil) |
| 75 | + NotificationCenter.default.removeObserver(self, name: UIResponder.keyboardDidHideNotification, object: nil) |
| 76 | + } |
| 77 | + |
| 78 | + private func animate(curve: Int, duration: Double, _ block: @escaping (() -> Void)) { |
| 79 | + let animationCurve = UIView.AnimationCurve(rawValue: curve) ?? .linear |
| 80 | + let animationOptions = UIView.AnimationOptions(rawValue: UInt(animationCurve.rawValue << 16)) |
| 81 | + |
| 82 | + UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: block, completion: nil) |
38 | 83 | } |
39 | 84 |
|
40 | | - @objc func onKeyboardShow(_ notification: Notification) { |
| 85 | + @objc func keyboardWillShow(_ notification: Notification) { |
41 | 86 | guard |
42 | 87 | let userInfo = notification.userInfo, |
43 | | - let keyboardFrameFinal = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect, |
44 | 88 | let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double, |
45 | | - let curveInt = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int |
| 89 | + let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int, |
| 90 | + let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect |
46 | 91 | else { |
47 | 92 | return |
48 | 93 | } |
49 | 94 |
|
50 | | - let animationCurve = UIView.AnimationCurve(rawValue: curveInt) ?? .linear |
51 | | - let animationOptions = UIView.AnimationOptions(rawValue: UInt(animationCurve.rawValue << 16)) |
52 | | - |
53 | | - UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: { |
54 | | - self.showAnimationBlock?(keyboardFrameFinal) |
55 | | - }, completion: nil) |
| 95 | + self.animate(curve: curve, duration: duration) { |
| 96 | + self.willShow?(frame) |
| 97 | + } |
56 | 98 | } |
57 | 99 |
|
58 | | - @objc func onKeyboardHide(_ notification: Notification) { |
| 100 | + @objc func keyboardDidShow(_ notification: Notification) { |
59 | 101 | guard |
60 | 102 | let userInfo = notification.userInfo, |
61 | 103 | let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double, |
62 | | - let curveInt = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int |
| 104 | + let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int, |
| 105 | + let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect |
63 | 106 | else { |
64 | 107 | return |
65 | 108 | } |
66 | 109 |
|
67 | | - let animationCurve = UIView.AnimationCurve(rawValue: curveInt) ?? .linear |
68 | | - let animationOptions = UIView.AnimationOptions(rawValue: UInt(animationCurve.rawValue << 16)) |
69 | | - |
70 | | - UIView.animate(withDuration: duration, delay: 0, options: animationOptions, animations: { |
71 | | - self.hideAnimationBlock?() |
72 | | - }, completion: nil) |
| 110 | + self.animate(curve: curve, duration: duration) { |
| 111 | + self.didShow?(frame) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @objc func keyboardWillHide(_ notification: Notification) { |
| 116 | + guard |
| 117 | + let userInfo = notification.userInfo, |
| 118 | + let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double, |
| 119 | + let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int |
| 120 | + else { |
| 121 | + return |
| 122 | + } |
| 123 | + self.animate(curve: curve, duration: duration) { |
| 124 | + self.willHide?() |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + @objc func keyboardDidHide(_ notification: Notification) { |
| 129 | + guard |
| 130 | + let userInfo = notification.userInfo, |
| 131 | + let duration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? Double, |
| 132 | + let curve = userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? Int |
| 133 | + else { |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + self.animate(curve: curve, duration: duration) { |
| 138 | + self.didHide?() |
| 139 | + } |
73 | 140 | } |
74 | 141 | } |
0 commit comments