Search code examples
ioscocoa-touchios5core-animationuikeyboard

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?


Solution

  • 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.