1

I am doing an animation to show the text view on top keyboard when UIKeyboardWillShowNotification is being broadcasted like following

-(void) keyboardWillShow:(NSNotification *)notification{
    // get keyboard size and loctaion
    NSDictionary *info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    NSNumber *duration = [notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];

    __block CGRect containerFrame = containerView.frame;

    containerFrame.origin.y = self.view.bounds.size.height - (kbSize.height + containerFrame.size.height);
    [UIView animateWithDuration:[duration doubleValue]
                          delay:0
                        options:UIViewAnimationOptionBeginFromCurrentState
                     animations:^{
                         containerView.frame = containerFrame;
                     } completion:^(BOOL finished) {
                         nil;
                     }
     ];
}

An animation is not good enough.If you can give it a try and will see small issue at the end of animation.

I googled and found out that there is another way to do an animation and it works perfectly :

-(void) keyboardWillShow:(NSNotification *)note{
    // get keyboard size and loctaion
    CGRect keyboardBounds;
    [[note.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue: &keyboardBounds];
    NSNumber *duration = [note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey];
    NSNumber *curve = [note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey];

    // Need to translate the bounds to account for rotation.
    keyboardBounds = [self.view convertRect:keyboardBounds toView:nil];

    // get a rect for the textView frame
    CGRect containerFrame = containerView.frame;
    containerFrame.origin.y = self.view.bounds.size.height - (keyboardBounds.size.height + containerFrame.size.height);
    // animations settings
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:[duration doubleValue]];
    [UIView setAnimationCurve:[curve intValue]];

    // set views with new info
    containerView.frame = containerFrame;


    // commit animations
    [UIView commitAnimations];
}

However, according to the documentation, we are not recommended to use this animation in Ios 4 and later on...

I can not tell any differences between my codes compared to the latter one. Why my codes can not do perfect animation. Please help if you have any ideas.

3
  • Can you elaborate what "small issue at the end of animation" means? Jan 24, 2014 at 2:57
  • Unless I'm mistaken, the best way to do this kind of thing in iOS 7 is to use Autolayout. I'm pretty sure there's a constraint you can set that will have a view maintain its distance to the bottom of the screen when the keyboard isn't present, or to the top of the keyboard when it is present. Jan 24, 2014 at 2:57
  • @JohnEstropia:give it a try, you will see it.
    – tranvutuan
    Jan 24, 2014 at 3:00

1 Answer 1

4

It's the animation curve. The example you cite is matching its animation curve as well its duration to those of the keyboard. Your code doesn't. Your structure should look more like this:

NSNumber* curve = info[UIKeyboardAnimationCurveUserInfoKey]; // *
NSNumber* duration = info[UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:duration.floatValue delay:0
                    options:curve.integerValue << 16 // *
                 animations: ^{ // ...
3
  • Regardless of my previous comment, it will perfectly. thanks.
    – tranvutuan
    Jan 24, 2014 at 3:11
  • @Matt, I haven't been satisfied with the animation curve in my keyboard animation code. I've tried using the curve value from the UIKeyboardAnimationCurveUserInfoKey directly. How did you figure out that you needed to shift it left 16 bits? And even when I try that my animation doesn't match the keyboard animation exactly. :(
    – Duncan C
    Jan 24, 2014 at 16:10
  • @DuncanC I looked at the documentation.
    – matt
    Jan 24, 2014 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.