iOS自动布局动画

时间:2014-08-19 07:13:38

标签: ios animation autolayout

例如,当我在执行以下代码时单击按钮:

UIView *view = [[UIView alloc] init];
[self.view addSubview:view];
view.backgroundColor = [UIColor blackColor];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(@0);
    make.trailing.equalTo(@0);
    make.bottom.equalTo(@-100);
    make.height.equalTo(@320);
}];


[view mas_updateConstraints:^(MASConstraintMaker *make) {
    make.bottom.equalTo(@-200);
}];

[UIView animateWithDuration:0.3f animations:^{
    [self.view layoutIfNeeded];
}];

但动画很奇怪,不是我想要的。如果添加约束而不是立即执行动画,例如:

UIView *view = [[UIView alloc] init];
[self.view addSubview:view];
view.backgroundColor = [UIColor blackColor];
[view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.leading.equalTo(@0);
    make.trailing.equalTo(@0);
    make.bottom.equalTo(@-100);
    make.height.equalTo(@320);
}];


dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [view mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@-200);
    }];

    [UIView animateWithDuration:0.3f animations:^{
        [self.view layoutIfNeeded];
    }];

});

此时动画正常。糟糕,几乎忘了说,自动布局我使用Masonry

1 个答案:

答案 0 :(得分:1)

您还需要致电[self.view setNeedsLayout],以便在致电[self.view layoutIfNeeded]之前放弃当前的视图布局。