动画约束
提示
完整的动画示例请参阅 英文版文档。
Masonry 约束可以通过 UIView.animate 配合 layoutIfNeeded() 实现流畅动画。
基本动画
关键模式:
- 使用
mas_updateConstraints:或mas_remakeConstraints:更新约束 - 在动画块中调用
layoutIfNeeded()
::: code-group
[box mas_updateConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(superview).offset(50);
}];
[UIView animateWithDuration:0.3 animations:^{
[superview layoutIfNeeded];
}];
box.mas.updateConstraints { make in
make.center.equalTo(superview).offset(50)
}
UIView.animate(withDuration: 0.3) {
superview.layoutIfNeeded()
}
:::
提示
- 使用
updateConstraints仅更改已有约束的constant值 —— 性能更好。 - 使用
remakeConstraints当布局结构完全改变时(如横竖屏切换)。