I'm trying to understand the new keyboard animation in iOS 7.0 on the iPhone 5 Simulator. I want to resize my UITableView
when the keyboard appears, but I can't get the right animation details.
I'm using the information from the NSNotification
object, when the keyboard appears or disappears.
Here is my log:
Move keyboard from {{0, 920}, {320, 216}} to {{0, 352}, {320, 216}}
with duration: 0.400000
and animation curve: 7
UIViewAnimationCurveEaseInOut = 0
UIViewAnimationCurveEaseIn = 1
UIViewAnimationCurveEaseOut = 2
UIViewAnimationCurveLinear = 3
The animation curve is an unknown value, what should I do?
In iOS 7, the keyboard uses a new, undocumented animation curve. While some have noted that using an undocumented value for the animation option, I prefer to use the following:
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 anUIViewAnimationOptions
. 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.Now I found the solution. The animation starts from the point
{0, 920}
to{0, 352}
. The problem was that theUITableView
object started with a size of{160, 568}
, so I changed the size of theUITableView
to{160, 920}
before the animation was started.Concerning to the unknown animation curve, I just set the parameter to
animationCurve << 16
to convert it from a view animation curve to a view animation option.The value is not equal to the linear, ease in, ease out and ease inout animation curve.
Here is my code:
and:
You can use
animateWithDuration
block and set curve inside it. It's clean and work well.Happy coding!
UPDATE
You can use a simple UIViewController category written by me https://github.com/Just-/UIViewController-KeyboardAnimation
Register for the notification:
Respond by animating a change to the frame.origin.y of the view.
To use the same animation as keyboard has, you have to use undocumented Curve option.
I found that actually method
animateWithDuration:delay:usingSpringWithDamping:initialSpringVelocity:options:animations:completion:
is the new way that all animations are done in iOS7 and iOS8 now. You just make right duration, damping and velocity and you will get the same effect, but even you can change speed/time.Use
UIKeyboardWillChangeFrameNotification
instead, because some international keyboards, like the Chinese keyboard, change height during use. Also this code gives you the correct heights for the keyboard, even in landscape mode. (Note: the code below is for Autolayout)