之前在寫代碼的時(shí)候發(fā)現(xiàn)使用了Masonry來做頁面布局的情況下,通過UIView的animateWithDuration 方法來添加動(dòng)畫是不會(huì)有效果的摔踱,那么應(yīng)該如何實(shí)現(xiàn)動(dòng)畫效果呢?
比如在點(diǎn)擊某個(gè)按鈕的時(shí)候需要更新約束璃弄,那么可以執(zhí)行下面的方法:
- (void)updateLayout {
// 告訴當(dāng)前的頁面需要更新約束
[self.view setNeedsUpdateConstraints];
// 添加動(dòng)畫效果陕靠,3s執(zhí)行完畢
[UIView animateWithDuration:3 animations:^{
// 更新約束
if (self.accountTextField.isShowKeyboard) {
[self.titleImage mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(0.f);
}];
[self.accountTextField mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).with.offset(SCREEN_SIZE_HEIGHT * 0.198);
}];
} else {
CGFloat scale = iPad ? 1.5f : 1.f;
[self.titleImage mas_updateConstraints:^(MASConstraintMaker *make) {
make.height.mas_equalTo(65.f * scale);
}];
[self.accountTextField mas_updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.view.mas_top).with.offset(SCREEN_SIZE_HEIGHT * 0.35);
}];
}
// 告知發(fā)生變化的視圖的父視圖需要重新繪制
[self.titleImage.superview layoutIfNeeded];
[self.accountTextField.superview layoutIfNeeded];
}];
}