Question

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.

Was it helpful?

Solution

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: ^{ // ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top