Search code examples
objective-cuitableviewanimationkeyboardios7

iOS 7 - Keyboard animation


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?


Solution

  • Now I found the solution. The animation starts from the point {0, 920} to {0, 352}. The problem was that the UITableView object started with a size of {160, 568}, so I changed the size of the UITableView 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:

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(_keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    

    and:

    - (void)keyboardWillShow:(NSNotification *)aNotification {
        NSDictionary *userInfo = aNotification.userInfo;
    
        //
        // Get keyboard size.
    
        NSValue *beginFrameValue = userInfo[UIKeyboardFrameBeginUserInfoKey];
        CGRect keyboardBeginFrame = [self.view convertRect:beginFrameValue.CGRectValue fromView:nil];
    
        NSValue *endFrameValue = userInfo[UIKeyboardFrameEndUserInfoKey];
        CGRect keyboardEndFrame = [self.view convertRect:endFrameValue.CGRectValue fromView:nil];
    
        //
        // Get keyboard animation.
    
        NSNumber *durationValue = userInfo[UIKeyboardAnimationDurationUserInfoKey];
        NSTimeInterval animationDuration = durationValue.doubleValue;
    
        NSNumber *curveValue = userInfo[UIKeyboardAnimationCurveUserInfoKey];
        UIViewAnimationCurve animationCurve = curveValue.intValue;
    
        //
        // Create animation.
    
        CGRect tableViewFrame = self.tableView.frame;
        bTableViewFrame.size.height = (keyboardBeginFrame.origin.y - tableViewFrame.origin.y);
        self.tableView.frame = tableViewFrame;
    
        void (^animations)() = ^() {
            CGRect tableViewFrame = self.tableView.frame;
            tableViewFrame.size.height = (keyboardEndFrame.origin.y - tableViewFrame.origin.y);
            self.tableView.frame = tableViewFrame;
        };
    
        //
        // Begin animation.
    
        [UIView animateWithDuration:animationDuration
                              delay:0.0
                            options:(animationCurve << 16)
                         animations:animations
                         completion:nil];
    }