Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

CGRect keyboardBeginFrame;
[[note.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] getValue:&keyboardBeginFrame];
self.doneKeyboardButton.frame = CGRectMake(0, (keyboardBeginFrame.origin.y + keyboardBeginFrame.size.height) - 53, 106, 53);
[[[[UIApplication sharedApplication] windows] lastObject] addSubview:self.doneKeyboardButton];

CGPoint newCenter = CGPointMake(self.doneKeyboardButton.superview.frame.origin.x + self.doneKeyboardButton.frame.size.width/2,
                                self.doneKeyboardButton.superview.frame.size.height - self.doneKeyboardButton.frame.size.height/2);

[UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]-.02
                      delay:.0
                    options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                 animations:^{
                     self.contentView.frame = CGRectOffset(self.contentView.frame, 0, -TextFieldViewMovement);
                     self.doneKeyboardButton.center = newCenter;
                 }
                 completion:nil];

However, this has stopped working on iOS7. It seems like the values returned are no longer exactly correct, and the Done button no longer exactly mimics the Keyboard display animation.

like image 481
Matt Foley Avatar asked Sep 16 '13 20:09

Matt Foley


2 Answers

In iOS 7, the keyboard uses a new, undocumented animation curve. While some have noted that using an undocumented value for the animation option works, I prefer to use the following:

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] unsignedIntegerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];

// work

[UIView commitAnimations];

While block based animations are the recommendation, the animation curve returned from the keyboard notification is an UIViewAnimationCurve, while the option you would need to pass to block based animations is an UIViewAnimationOptions. Using the traditional UIView animation methods allows you to pipe the value directly in. Most importantly, this will use the new undocumented animation curve (integer value of 7) and cause the animation to match the keyboard. And, it will work just as well on iOS 6 and 7.

like image 83
David Beck Avatar answered Oct 20 '22 07:10

David Beck


I had the same issue and managed to get the animation working with the following parameters for iOS 7:

    [UIView animateWithDuration:0.5
                          delay:0
         usingSpringWithDamping:500.0f
          initialSpringVelocity:0.0f
                        options:UIViewAnimationOptionCurveLinear
                     animations:animBlock
                     completion:completionBlock];

EDIT: these values were obtained through debug, and can change with new iOS versions. @DavidBeck's answer works for me in iOS 7 also so I'm linking it here.

like image 23
ZeCodea Avatar answered Oct 20 '22 06:10

ZeCodea