Andrew Lauer Barinov
Andrew Lauer Barinov

Reputation: 5754

Getting a UIKeyboard related animation just right

I am currently using keyboardWillShow and keyboardWillHide notifications to animate a user interface element in response to a UIKeyboard appearing on the screen.

I am calling a method

- (void) animateElement: (UIToolbar*) toolbar up: (BOOL) up

inside of the selectors that get called with the notifications are broadcasted. The method creates the animations and adds them to the appropriate layer.

Everything works well, but visually the experience is disappointing because the keyboard appears before the element is translated and therefore the transition feels abrupt, as the element momentarily disappears from view and then reappears at the right place. The animation is basically obscured by the keyboard animation.

Other apps like Path have a better transition, that starts sooner and is not obscured by the keyboard. Does anyone how it's accomplished? I can't think of a way to call it sooner, because the prior to receiving the notifications, the app has no way of knowing that the keyboard is appearing, right?

Upvotes: 0

Views: 2872

Answers (2)

Fervus
Fervus

Reputation: 720

For anyone who is still wondering on the real values, my code looks like this:

[UIView animateWithDuration:0.25f
                      delay:0.0f
                    options:UIViewAnimationOptionCurveEaseInOut
                 animations:^
 {
     CGRect sendFrame = sendMessageToolbar.frame;
     sendFrame.origin.y = self.tableView.frame.size.height - KEYBOARD_HEIGHT;
     sendMessageToolbar.frame = sendFrame;
 }
                 completion:^(BOOL finished)
 {

 }];

Take it with slow animations enabled in emulator and you will see it matches perfectly.

Upvotes: 3

The Lazy Coder
The Lazy Coder

Reputation: 11828

check out the input accessory view

self.myTextField.inputAccessoryView = self.uiViewforKeyboardAttachment;//Can be any uiview

this will automatically attach to the kb for this text field.

That may alleviate your need to try to auto animate the toolbar.

Or you can use the animation duration within the notification.userInfo it has a key for UIKeyboardAnimationDurationUserInfoKey which will be an NSNumber. the doubleValue will be the duration of the animation.

that dictionary will also have UIKeyboardAnimationCurveUserInfoKey, UIKeyboardFrameBeginUserInfoKey and UIKeyboardFrameEndUserInfoKey

All of those will help with the connection to the animation curve and start and end points.

I believe these are relative to the window or the first view within the window however. so be sure to convert to the view you need to know about.

Upvotes: 3

Related Questions