// UIView如果使用AutoLayout 必須寫此方法
+ (BOOL)requiresConstraintBasedLayout
{
return YES;
}
//按鈕的動(dòng)態(tài)效果修改時(shí) 此方法為 系統(tǒng)自帶方法
// 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); //按鈕的最大寬度 <= 當(dāng)前self的寬度
make.height.lessThanOrEqualTo(self); //按鈕的最大高度 <= 當(dāng)前self的寬度
}];
//according to apple super should be called at end of method
[super updateConstraints]; //蘋果規(guī)定最后調(diào)用這個(gè)
}
//按鈕觸發(fā)的時(shí)候
1. [self setNeedsUpdateConstraints]; //必須調(diào)用
2. [self updateConstraintsIfNeeded]; //更新約束
3. [UIView animateWithDuration:0.4 animations:^{
[self layoutIfNeeded];
}]; //動(dòng)畫效果
//代碼演示
- (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];
}];
}