预先定义一个约束的全局变量,用来接收需要变化的约束
@property (nonatomic, strong) MASConstraint *hudViewTop;
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[windowaddSubview:self.hudView];
[self.hudView makeConstraints:^(MASConstraintMaker *make) {
self.hudViewTop = make.top.equalTo(window.top).offset(-70);
make.left.equalTo(window.left).offset(20);
make.right.equalTo(window.right).offset(-20);
make.height.equalTo(@70);
}];
- (void)_showAndHideHud{//出现隐藏头部hud
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[self.hudViewTop uninstall];//先销毁约束
[UIView animateWithDuration:1.0 animations:^{//修改约束驾到动画的过程中
[self.hudView updateConstraints:^(MASConstraintMaker *make) {
self.hudViewTop = make.top.equalTo(window.top).offset(NAVBAR_HEIGHT/2);
}];
[window layoutIfNeeded];//一定要加,---父视图--- 刷新
}];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{//gcd 延迟调用
[self.hudViewTop uninstall];
[UIView animateWithDuration:1.0 animations:^{
[self.hudView updateConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(window.top).offset(-70);
}];
[windowlayoutIfNeeded];
}];
});
}