lingchen
lingchen

Reputation: 473

iOS autolayout animation

For example, when I click on the button when the following code execution:

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

But the animation is very strange, not what I want. If you add a constraint not immediately executed animation, such as this:

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

});

This time animation to normal. Oops, almost forgot to say, autolayout I use the Masonry

Upvotes: 1

Views: 1672

Answers (1)

Mohd Iftekhar Qurashi
Mohd Iftekhar Qurashi

Reputation: 2405

you also need to call [self.view setNeedsLayout] in order to discard current view layout before calling [self.view layoutIfNeeded].

Upvotes: 1

Related Questions