美文网首页iOS大咖说
iOS开发中使用Masonry更新约束产生动画效果

iOS开发中使用Masonry更新约束产生动画效果

作者: 梁森的简书 | 来源:发表于2019-04-12 09:58 被阅读4次

在苹果提供的动画方法block中直接使用Masonry更新视图的约束,并不能像使用frame那样产生动画效果。
代码:

  [UIView animateWithDuration:0.75 animations:^{
    [self.redBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(150));
    }];
    
}];

我们需要在更新约束之后还要调用该视图父视图的一个layoutIfNeeded方法才能产生动画效果。
代码对比:

  // 使用Masonry
[UIView animateWithDuration:0.75 animations:^{
    [self.redBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@(150));
    }];
    [self.redBtn.superview layoutIfNeeded];
}];

// 使用frame
[UIView animateWithDuration:0.75 animations:^{
    self.yellowBtn.frame = CGRectMake(100, 300, 100, 100);
}];

效果对比:


动画.GIF

重要方法: [self.redBtn.superview layoutIfNeeded];

****本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。😊***

相关文章

网友评论

    本文标题:iOS开发中使用Masonry更新约束产生动画效果

    本文链接:https://www.haomeiwen.com/subject/wwsqwqtx.html