gpt4 book ai didi

iOS自动布局动画

转载 作者:行者123 更新时间:2023-11-29 12:39:28 26 4
gpt4 key购买 nike

比如当我点击按钮的时候执行如下代码:

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

});

这时候动画就正常了。糟糕,差点忘了说,autolayout我用的是Masonry

最佳答案

您还需要调用 [self.view setNeedsLayout] 以便在调用 [self.view layoutIfNeeded] 之前丢弃当前 View 布局。

关于iOS自动布局动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25377572/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com