美文网首页约束iOS DeveloperiOS学习
代码片段之Masonry 按钮放大缩小动画简单实践

代码片段之Masonry 按钮放大缩小动画简单实践

作者: 东引瓯越 | 来源:发表于2015-05-08 10:19 被阅读2903次

// 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];
    }];
}

相关文章

网友评论

本文标题:代码片段之Masonry 按钮放大缩小动画简单实践

本文链接:https://www.haomeiwen.com/subject/auywfttx.html