在 iOS 代码手写控件一族中,在使用frame来动画更新控件frame是再熟悉不过的了:
[UIView animateWithDuration:0.5 animations:^{
view.frame = CGRectMake();
}];
但在引入Masonry后,使用如下代码来更新控件约束后,但无法看到控件位置更新的动画:
[UIView animateWithDuration:0.5 animations:^{
[view mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo();
}];
}];
正确的姿势:
[view mas_updateConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(weakSelf.view);
}];
[view setNeedsUpdateConstraints]; //通知系统视图中的约束需要更新
[view updateConstraintsIfNeeded]; //
[UIView animateWithDuration:0.5 animations:^{
[self.view layoutIfNeeded]; //使约束立即生效
}];