Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the animation speed of the keyboard appearing in iOS8?

The following is an animation for a textField and toolBar which move upward when the keyboard appears.

    baseConstraint.constant = 211     self.view.setNeedsUpdateConstraints()      UIView.animateWithDuration(0.30, animations: {         self.view.layoutIfNeeded()         }) 

It is close but not quite identical. How would you modify the above animation?

Edit:

Here is the final code using the answer below!

   func keyboardWillShow(aNotification: NSNotification)    {          let duration = aNotification.userInfo.objectForKey(UIKeyboardAnimationDurationUserInfoKey) as Double         let curve = aNotification.userInfo.objectForKey(UIKeyboardAnimationCurveUserInfoKey) as UInt          self.view.setNeedsLayout()         baseConstraint.constant = 211         self.view.setNeedsUpdateConstraints()          UIView.animateWithDuration(duration, delay: 0, options: UIViewAnimationOptions.fromMask(curve), animations: {         self.view.layoutIfNeeded()         }, completion: {         (value: Bool) in println()         }) } 
like image 412
user3784622 Avatar asked Jul 24 '14 00:07

user3784622


2 Answers

You can get the animation duration and the animation curve from the userInfo dictionary on the keyboardWillShow: notifications.

First register for the notification

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 

Then get the values from the notifications userInfo keys.

- (void)keyboardWillShow:(NSNotification*)notification {     NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];     NSNumber *curve = [notification.userInfo objectForKey: UIKeyboardAnimationCurveUserInfoKey];     // Do stuff with these values. } 

There are a lot more of these keys, and you can also get them from the UIKeyboardWillDismiss notification.

This functionality is available all the way back to iOS 3.0 :D

Heres the docs:

https://developer.apple.com/library/ios/documentation/uikit/reference/UIWindow_Class/UIWindowClassReference/UIWindowClassReference.html#//apple_ref/doc/constant_group/Keyboard_Notification_User_Info_Keys

Swift Version

NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)  @objc func keyboardWillShow(_ notification: Notification) {     let duration = notification.userInfo?[UIResponder.keyboardAnimationDurationUserInfoKey]     let curve = notification.userInfo?[UIResponder.keyboardAnimationCurveUserInfoKey] } 
like image 79
Joel Bell Avatar answered Sep 28 '22 15:09

Joel Bell


The answer with the variable duration is right and work iOS 3 to 8, but with the new version of Swift, the answer's code is not working anymore. Maybe it is a mistake on my side, but I have to write:

let duration = aNotification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as Double let curve = aNotification.userInfo![UIKeyboardAnimationCurveUserInfoKey] as UInt  self.view.setNeedsLayout() //baseConstraint.constant = 211 self.view.setNeedsUpdateConstraints()  UIView.animateWithDuration(duration, delay: 0.0, options: UIViewAnimationOptions(curve), animations: { _ in     //self.view.layoutIfNeeded() }, completion: { aaa in     //(value: Bool) in println() }) 

Looks like objectForKey is not working anymore and conversion is more strict.

like image 45
Dam Avatar answered Sep 28 '22 15:09

Dam