Synchronizing Animations in keyboardWillShow keyboardWillHide — Hardware Keyboard & Virtual Keyboard Simultaneously

前端 未结 2 902
青春惊慌失措
青春惊慌失措 2020-12-29 08:25

Preamble

So I have an application featuring a chat section, and I\'m synchronizing the animation of the keyboard hiding and showing with the rise and fall of the

相关标签:
2条回答
  • 2020-12-29 08:41

    Since the animation curve Apple sends you in the keyboard notification does not have a corresponding UIViewAnimationOption bit, you need to drop down to old-school non-block animations and use the curve directly:

    NSTimeInterval duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [note.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    [UIView beginAnimations:@"SomeAnimationID" context:NULL];
    [UIView setAnimationCurve:curve];
    [UIView setAnimationDuration:duration];
    // Animation code
    [UIView commitAnimations];
    
    0 讨论(0)
  • 2020-12-29 08:53

    keyboardWillShow:

    NSDictionary *info = [notification userInfo];
    CGRect keyboardFrame = [info[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    NSTimeInterval duration = [info[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [info[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    [self layoutIfNeeded];
    [UIView animateWithDuration:duration delay:0 options:(curve << 20) animations:^{
        [self.keyboardConstraint setConstant:keyboardFrame.size.height];
        [self layoutIfNeeded];
    } completion:nil];
    
    0 讨论(0)
提交回复
热议问题