AutoLayout中NSLayoutConstraint动画实现

问题描述

通过修改自动布局控件NSLayoutConstraint的constant属性可以实现控制位置的修改,但是测试发现UIView提供动画的代码没有起作用

+ (void)animateWithDuration:(NSTimeInterval)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options animations:(void (^)(void))animations completion:(void (^ __nullable)(BOOL finished))completion NS_AVAILABLE_IOS(4_0);

解决方式

需要在目标动画的控件上添加layoutIfNeeded函数的调用

- (void) keyboardInputTextFieldWillShow:(NSNotification *) notification {
    
    
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    double animationDuration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    _keyboardAnimationDuration = animationDuration;
    
    [UIView animateWithDuration:animationDuration delay:0.0f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.topConstraintHeight.constant = CGRectGetHeight(self.view.frame) - CGRectGetHeight(_inputView.frame) - kbSize.height;
        [self.inputView layoutIfNeeded];  // add 
    } completion:^(BOOL finished) {
        
    }];

}

你可能感兴趣的:(AutoLayout中NSLayoutConstraint动画实现)