MasonryMasonry
指南
  • Objective-C API
  • Swift API
示例
更新日志
  • English
  • 简体中文
GitHub
指南
  • Objective-C API
  • Swift API
示例
更新日志
  • English
  • 简体中文
GitHub
  • 示例

    • 常见布局模式
    • 动画约束
    • ScrollView 布局

动画约束

提示

完整的动画示例请参阅 英文版文档。

Masonry 约束可以通过 UIView.animate 配合 layoutIfNeeded() 实现流畅动画。

基本动画

关键模式:

  1. 使用 mas_updateConstraints: 或 mas_remakeConstraints: 更新约束
  2. 在动画块中调用 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 当布局结构完全改变时(如横竖屏切换)。
在 GitHub 上编辑此页
上次更新: 2026/3/29 12:09
贡献者: samzhjiang
Prev
常见布局模式
Next
ScrollView 布局