How to mimic Keyboard animation on iOS 7 to add “Done” button to numeric keyboard?

前端 未结 7 996
故里飘歌
故里飘歌 2020-12-02 10:09

I had been doing something like this to mimic the keyboard animation on older version of iOS.

CGRect keyboardBeginFrame;
[[note.userInfo objectForKey:UIKeybo         


        
相关标签:
7条回答
  • 2020-12-02 10:58

    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()
    }
    
    0 讨论(0)
提交回复
热议问题