Last active
May 16, 2017 19:15
-
-
Save joshuawright11/8dba079b7540347725a2881ce826495b to your computer and use it in GitHub Desktop.
Animate on keyboard show and dismiss
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| override func viewDidLoad() { | |
| // ... | |
| Utils.registerForNotification(self, selector: #selector(MyViewController.keyboardShow(_:)), name: NSNotification.Name.UIKeyboardWillShow.rawValue) | |
| Utils.registerForNotification(self, selector: #selector(MyViewController.keyboardHide(_:)), name: NSNotification.Name.UIKeyboardWillHide.rawValue) | |
| // ... | |
| } | |
| func keyboardShow(_ notification: Notification){ | |
| let userInfo = (notification as NSNotification).userInfo | |
| // Get animation curve of keyboard display | |
| let duration = userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber | |
| let curve = userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber | |
| let adjustedCurve = UIViewAnimationOptions(rawValue: (curve.uintValue << 16)) | |
| UIView.animate(withDuration: duration.doubleValue, delay: 0, options: adjustedCurve, animations: { | |
| // animate tableview to offset | |
| }, completion: nil) | |
| } | |
| func keyboardHide(_ notification: Notification){ | |
| let userInfo = (notification as NSNotification).userInfo | |
| let duration = userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber | |
| let curve = userInfo![UIKeyboardAnimationCurveUserInfoKey] as! NSNumber | |
| let adjustedCurve = UIViewAnimationOptions(rawValue: (curve.uintValue << 16)) | |
| UIView.animate(withDuration: duration.doubleValue, delay: 0, options: adjustedCurve, animations: { | |
| // animate tableview to normal position | |
| }, completion: nil) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment