Skip to content

Commit 4fb8a4b

Browse files
committed
ViewController: improve keyboard notification handlers
retrieve animation curve info to flawlessly show/hide keyboard with $keyboardHeight
1 parent e1031b1 commit 4fb8a4b

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

Classes/Controllers/ViewController.swift

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,36 @@ open class ViewController: UIViewController {
6464

6565
var keyboardWasShownAtLeastOnce = false
6666

67-
open func keyboardAppeared(_ height: CGFloat, _ animationDuration: TimeInterval, _ inThisController: Bool) {
67+
open func keyboardAppeared(_ height: CGFloat, _ animationDuration: TimeInterval, _ animationCurve: UIView.AnimationCurve?, _ inThisController: Bool) {
6868
keyboardWasShownAtLeastOnce = true
6969
if inThisController {
7070
if #available(iOS 10.0, *) {
71-
UIViewPropertyAnimator(duration: animationDuration, curve: .linear) {
71+
UIViewPropertyAnimator(duration: animationDuration, curve: animationCurve ?? .linear) {
7272
self.keyboardHeight = height
7373
self.view.layoutIfNeeded()
7474
}.startAnimation()
7575
} else {
7676
self.keyboardHeight = height
77-
self.view.layoutIfNeeded()
77+
UIView.animate(withDuration: animationDuration) {
78+
self.view.layoutIfNeeded()
79+
}
7880
}
7981
}
8082
}
8183

82-
open func keyboardDisappeared(_ animationDuration: TimeInterval, _ inThisController: Bool) -> Bool {
84+
open func keyboardDisappeared(_ animationDuration: TimeInterval, _ animationCurve: UIView.AnimationCurve?, _ inThisController: Bool) -> Bool {
8385
guard keyboardWasShownAtLeastOnce else { return false }
8486
if inThisController {
8587
if #available(iOS 10.0, *) {
86-
UIViewPropertyAnimator(duration: animationDuration, curve: .linear) {
88+
UIViewPropertyAnimator(duration: animationDuration, curve: animationCurve ?? .linear) {
8789
self.keyboardHeight = 0
8890
self.view.layoutIfNeeded()
8991
}.startAnimation()
9092
} else {
9193
self.keyboardHeight = 0
92-
self.view.layoutIfNeeded()
94+
UIView.animate(withDuration: animationDuration) {
95+
self.view.layoutIfNeeded()
96+
}
9397
}
9498
}
9599
return true
@@ -106,13 +110,21 @@ extension ViewController {
106110
let keyboardRectangle = keyboardFrame.cgRectValue
107111
guard keyboardRectangle.height > 0 else { return }
108112
guard let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else { return }
109-
keyboardAppeared(keyboardRectangle.height, animationDuration, isSubscribedToKeyboardNotifications)
113+
var animationCurve: UIView.AnimationCurve?
114+
if let animationCurveInt = (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue {
115+
animationCurve = UIView.AnimationCurve(rawValue: animationCurveInt)
116+
}
117+
keyboardAppeared(keyboardRectangle.height, animationDuration, animationCurve, isSubscribedToKeyboardNotifications)
110118
}
111119

112120
@objc
113121
private func keyboardWillDisappear(notification: NSNotification) {
114122
guard let userInfo = notification.userInfo else { return }
115123
guard let animationDuration = userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? TimeInterval else { return }
116-
_ = keyboardDisappeared(animationDuration, isSubscribedToKeyboardNotifications)
124+
var animationCurve: UIView.AnimationCurve?
125+
if let animationCurveInt = (userInfo[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber)?.intValue {
126+
animationCurve = UIView.AnimationCurve(rawValue: animationCurveInt)
127+
}
128+
_ = keyboardDisappeared(animationDuration, animationCurve, isSubscribedToKeyboardNotifications)
117129
}
118130
}

0 commit comments

Comments
 (0)