关于 masonry 使用 mas_updateConstraints 添加动画的说明

今天在开发一个功能模块的时候,需要给某个按钮添加右滑手势,动画的显示删除按钮。

约束动画注意.gif

这里就涉及到了需要更新约束的问题。

但是一开始使用更新约束,动画无效。

代码是这么写的。

- (void)swipeShowGesture:(UISwipeGestureRecognizer *)gesture {
    [UIView animateWithDuration:0.25 animations:^{
        [_deleteButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.offset(0);
        }];
        [_deleteButton layoutIfNeeded];
        
    }];
}

后来换成了这种写法

- (void)swipeShowGesture:(UISwipeGestureRecognizer *)gesture {
    [UIView animateWithDuration:0.25 animations:^{
        [_deleteButton mas_updateConstraints:^(MASConstraintMaker *make) {
            make.right.offset(0);
        }];
        [self layoutIfNeeded];
    }];
}

效果就出来了。

分析了一下:
对于 right、top、bottom、left 等位置的约束,它们是相对于父视图而言的。所以,必须是父视图更新约束才行。也就是 [self layoutIfNeeded];

而对于 width,heigth 等大小的约束,是控制视图本身。可以使用 [_deleteButton layoutIfNeeded] ;

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

推荐阅读更多精彩内容