// UIView如果使用AutoLayout 必须写此方法
+ (BOOL)requiresConstraintBasedLayout
{
return YES;
}
//按钮的动态效果修改时 此方法为 系统自带方法
// this is Apple's recommended place for adding/updating constraints
- (void)updateConstraints {
[self.growingButton updateConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self);
make.width.equalTo(@(self.buttonSize.width)).priorityLow();
make.height.equalTo(@(self.buttonSize.height)).priorityLow();
make.width.lessThanOrEqualTo(self); //按钮的最大宽度 <= 当前self的宽度
make.height.lessThanOrEqualTo(self); //按钮的最大高度 <= 当前self的宽度
}];
//according to apple super should be called at end of method
[super updateConstraints]; //苹果规定最后调用这个
}
//按钮触发的时候
1. [self setNeedsUpdateConstraints]; //必须调用
2. [self updateConstraintsIfNeeded]; //更新约束
3. [UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}]; //动画效果
//代码演示
- (void)didTapGrowButton:(UIButton *)button {
if (!self.zoom) {
self.buttonSize = CGSizeMake(self.buttonSize.width * 5, self.buttonSize.height * 6);
self.zoom = YES;
} else {
self.buttonSize = CGSizeMake(self.buttonSize.width / 5, self.buttonSize.height / 6);
self.zoom = NO;
}
// tell constraints they need updating
[self setNeedsUpdateConstraints];
// update constraints now so we can animate the change
[self updateConstraintsIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}];
}