I had been doing something like this to mimic the keyboard animation on older version of iOS.
CGRect keyboardBeginFrame;
[[note.userInfo objectForKey:UIKeybo
iOS 10+
Swift 5
A more modern alternative (iOS 10+ and Swift) is to use UIViewPropertyAnimator
, which not only accepts UIView.AnimationCurve
but is a more modern animator.
Most likely you'll be working with UIResponder.keyboardAnimationCurveUserInfoKey
, which we are to treat as an NSNumber
. The documentation for this key is (Apple's own notation, not mine):
public class let keyboardAnimationCurveUserInfoKey: String // NSNumber of NSUInteger (UIViewAnimationCurve)
Using this approach, we can eliminate any guesswork and just plug and play:
if let kbDuration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber,
let kbTiming = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] as? NSNumber, // doc says to treat as NSNumber
let timing = UIView.AnimationCurve.RawValue(exactly: kbTiming), // takes an NSNumber
let curve = UIView.AnimationCurve(rawValue: timing) // takes a raw value {
let duration = kbDuration.doubleValue
let animator = UIViewPropertyAnimator(duration: duration, curve: curve) {
// add animations
}
animator.startAnimation()
}