banner



How To Animate And Curve The View In Swift

This post presents an implementation of animations that animate alongside the iOS keyboard animation. Following the method in this post will allow your views to animate smoothly when the iOS keyboard shows and hides.

  1. Subscribe To Keyboard Show And Hibernate Notifications
  2. keyboardAnimationCurveUserInfoKey and UIViewPropertyAnimator
  3. Animate With The iOS Keyboard
  4. Example Keyboard Animation Implementation
  5. Move View With iOS Keyboard Animation

The first pace is to subscribe to the will evidence and will hibernate keyboard animations.

              class ViewController: UIViewController {     override func viewDidLoad() {         super.viewDidLoad()                  // Subscribe to Keyboard Will Show notifications         NotificationCenter.default.addObserver(             self,             selector: #selector(keyboardWillShow(_:)),             name: UIResponder.keyboardWillShowNotification,             object: zero         )          // Subscribe to Keyboard Volition Hide notifications         NotificationCenter.default.addObserver(             cocky,             selector: #selector(keyboardWillHide(_:)),             name: UIResponder.keyboardWillHideNotification,             object: nil         )     }      @objc     dynamic func keyboardWillShow(        _ notification: NSNotification     ) { }          @objc     dynamic func keyboardWillHide(         _ notification: NSNotification     ) { } }                          

keyboardAnimationCurveUserInfoKey and UIViewPropertyAnimator

The side by side step is to create a function that wraps the iOS keyboard animation. The iOS keyboard animation uses a custom animation curve that is not publically accessible. To breathing with the keyboard, extract the curve from the notification using the key UIResponder.keyboardAnimationCurveUserInfoKey and then use a UIViewPropertyAnimator to perform the animations.

In the following implementation, the animations block passed into animateWithKeyboard allows the caller to specify layout changes that should animate with the iOS keyboard animation. An example implementation is bachelor at the stop of this post.

              extension ViewController {     func animateWithKeyboard(         notification: NSNotification,          animations: ((_ keyboardFrame: CGRect) -> Void)?     ) {         // Extract the duration of the keyboard animation         permit durationKey = UIResponder.keyboardAnimationDurationUserInfoKey         permit duration = notification.userInfo![durationKey] as! Double                  // Extract the terminal frame of the keyboard         let frameKey = UIResponder.keyboardFrameEndUserInfoKey         let keyboardFrameValue = notification.userInfo![frameKey] equally! NSValue                  // Extract the curve of the iOS keyboard animation         let curveKey = UIResponder.keyboardAnimationCurveUserInfoKey         let curveValue = notification.userInfo![curveKey] equally! Int         let curve = UIView.AnimationCurve(rawValue: curveValue)!          // Create a holding animator to manage the animation         let animator = UIViewPropertyAnimator(             elapsing: duration,             curve: bend         ) {             // Perform the necessary animation layout updates             animations?(keyboardFrameValue.cgRectValue)                          // Required to trigger NSLayoutConstraint changes             // to breathing             self.view?.layoutIfNeeded()         }                  // Showtime the animation         animator.startAnimation()     } }                          

Animate With The iOS Keyboard

That's it! Using the animateWithKeyboard method allows custom animations to move with the iOS keyboard. For instance, a UITextField may movement up and down as the iOS keyboard shows and hides.

Example Keyboard Animation Implementation

              // ...      @objc     dynamic func keyboardWillShow(         _ notification: NSNotification     ) {         animateWithKeyboard(notification: notification) {              (keyboardFrame) in             let abiding = 20 + keyboardFrame.height             self.textFieldTrailingConstraint?.constant = abiding         }     }          @objc     dynamic func keyboardWillHide(         _ notification: NSNotification     ) {         animateWithKeyboard(notification: notification) {              (keyboardFrame) in             cocky.textFieldTrailingConstraint?.abiding = 20         }     }  // ...                          

Move View With iOS Keyboard Animation

Smooth Layout Changes With The iOS Keyboard Animation
Polish Layout Changes With The iOS Keyboard Animation

Source: https://www.advancedswift.com/animate-with-ios-keyboard-swift/

Posted by: smoothitery.blogspot.com

0 Response to "How To Animate And Curve The View In Swift"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel