Skip to content

Instantly share code, notes, and snippets.

@joshuawright11
Last active May 16, 2017 19:15
Show Gist options
  • Select an option

  • Save joshuawright11/8dba079b7540347725a2881ce826495b to your computer and use it in GitHub Desktop.

Select an option

Save joshuawright11/8dba079b7540347725a2881ce826495b to your computer and use it in GitHub Desktop.
Animate on keyboard show and dismiss
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