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) {
        
    }];

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容