iOS自动布局之Masonry第三弹

/ 0评 / 0

这一次学习一下 updateConstraints 和 remakeConstraints

先看一下 updateConstraints ,我们做一个能够放大的按钮来实验一下

- (void)updateConstraints{
    self.growingButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.growingButton setTitle:@"点我放大" forState:UIControlStateNormal];
    self.growingButton.layer.borderColor = UIColor.greenColor.CGColor;
    self.growingButton.layer.borderWidth = 3;
    
    [self.growingButton addTarget:self action:@selector(onGrowButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.growingButton];
    
    //记录增加的量
    self.scacle = 1.0;
    
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        //让按钮始终居中
        make.center.mas_equalTo(self.view);
        
        // 初始宽、高为100,优先级最低,就可以保证宽高不能超过屏幕。
        make.width.height.mas_equalTo(100 * self.scacle).priorityLow();
        // 最大放大到整个view
        make.width.height.lessThanOrEqualTo(self.view);
    }];
}

- (void)updateViewConstraints {
    [self.growingButton mas_updateConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.view);
        
        // 初始宽、高为100,优先级最低
        make.width.height.mas_equalTo(100 * self.scacle).priorityLow();
        // 最大放大到整个view
        make.width.height.lessThanOrEqualTo(self.view);
    }];
    
    [super updateViewConstraints];
}

- (void)onGrowButtonTaped:(UIButton *)sender {
    self.scacle += 0.5;
    
    // 告诉self.view约束需要更新,这个比较重要
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        //我们要使约束立即生效,就必须调用layoutIfNeeded此方法
        [self.view layoutIfNeeded];
    }];
}

效果就是每次你点击按钮的时候都会变大

Masonry 006

再看一下 remakeConstraints

- (void)remarkConstraints{
    self.remarkButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.remarkButton setTitle:@"点我展开" forState:UIControlStateNormal];
    self.remarkButton.layer.borderColor = UIColor.greenColor.CGColor;
    self.remarkButton.layer.borderWidth = 3;
    self.remarkButton.backgroundColor = [UIColor lightGrayColor];
    [self.remarkButton addTarget:self action:@selector(onGrowButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.remarkButton];
    self.isExpanded = NO;
    
    [self.remarkButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-350);
    }];
}

- (void)updateViewConstraints {
    // 这里使用update也是一样的。
    // remake会将之前的全部移除,然后重新添加
    WS(weakSelf);
    [self.remarkButton mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        if (weakSelf.isExpanded) {
            make.bottom.mas_equalTo(0);
        } else {
            make.bottom.mas_equalTo(-350);
        }
    }];
    
    [super updateViewConstraints];
}

- (void)onGrowButtonTaped:(UIButton *)sender {
    self.isExpanded = !self.isExpanded;
    if (!self.isExpanded) {
        [self.remarkButton setTitle:@"点我展开" forState:UIControlStateNormal];
    } else {
        [self.remarkButton setTitle:@"点我收起" forState:UIControlStateNormal];
    }
    
    // 告诉self.view约束需要更新,这个比较重要
    [self.view setNeedsUpdateConstraints];
    // 调用此方法告诉self.view检测是否需要更新约束,若需要则更新,下面添加动画效果才起作用
    [self.view updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        //我们要使约束立即生效,就必须调用layoutIfNeeded此方法
        [self.view layoutIfNeeded];
    }];
}

来看一下效果

Masonry 007

Masonry 008

这里就是说明一下我们之前提到的

mas_makeConstraints 只负责新增约束 Autolayout不能同时存在两条针对于同一对象的约束 否则会报错
mas_updateConstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
mas_remakeConstraints 则会清除之前的所有约束 仅保留最新的约束

代码请查看 http://git.oschina.net/zcb1603999/DCTest

评论已关闭。