ios oc 更新mansory加动画

143 阅读1分钟

和一般动画不同的是,使用Masonry这样放入到动画的block中不能直接让动画生效,只是直接造成控件位移,应当添加如下操作:

和一般动画不同的是,使用Masonry这样放入到动画的block中不能直接让动画生效,只是直接造成控件位移,经过尝试,应当添加如下操作
//放在主线程中
dispatch_async(dispatch_get_main_queue(), ^{
        //告知需要更改约束
        [self.view setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.3 animations:^{
            [self.contentLabel mas_updateConstraints:^(MASConstraintMaker *make) {

            make.left.top.mas_equalTo(20);

            make.right.mas_equalTo(-20);

            if (isExpanded) {

              make.bottom.mas_equalTo(0);

            } else {

              make.bottom.mas_equalTo(-40);

            }

          }];
            //告知父类控件绘制,不添加注释的这两行的代码无法生效
            [self.animateView.superview layoutIfNeeded];
        }];
    });

封装成:

- (void)cellHeightAnimation:(void (^)(void))animations{
    //和一般动画不同的是,使用Masonry这样放入到动画的block中不能直接让动画生效,只是直接造成控件位移,经过尝试,应当添加如下操作
    //放在主线程中
    dispatch_async(dispatch_get_main_queue(), ^{
        //告知需要更改约束
//        [self setNeedsUpdateConstraints];
        [self.contentView setNeedsUpdateConstraints];
        [self.contentView updateConstraintsIfNeeded];
            [UIView animateWithDuration:0.35 animations:^{
                if (animations) {
                    animations();
                }
                
                //告知父类控件绘制,不添加注释的这两行的代码无法生效
                [self.contentLabel.superview layoutIfNeeded];
            }];
        });
}