Ios 在-(void)updateConstraints中设置约束更改动画
我已经阅读了很多问题,为了使约束更改动画化,我们只需要Ios 在-(void)updateConstraints中设置约束更改动画,ios,autolayout,Ios,Autolayout,我已经阅读了很多问题,为了使约束更改动画化,我们只需要 self.someConstraint.constant = 0.f; [UIView animateWithDuration:0.5f animations:^{ [self layoutIfNeeded]; }]; 最近,我读到自定义视图子类应该在UIView上使用-(void)updateConstraints方法来执行约束更新,以防发生许多属性更改,导致您在多个位置执行上述代码块,这将导致许多不需要的布局过程 事实上,我
self.someConstraint.constant = 0.f;
[UIView animateWithDuration:0.5f animations:^{
[self layoutIfNeeded];
}];
最近,我读到自定义视图子类应该在UIView上使用-(void)updateConstraints
方法来执行约束更新,以防发生许多属性更改,导致您在多个位置执行上述代码块,这将导致许多不需要的布局过程
事实上,我喜欢这种更新所有约束的方法。一些简单的代码
- (void)updateConstraints
{
if(someCondition)
{
self.someConstraint.constant = 0.f;
}
//Maybe some other conditions or constant changes go
here based on the current state of your view.
[super updateConstraints];
}
此时,我的原始约束动画代码现在仅变为
[self setNeedsUpdateConstraints];
唯一的问题是我现在已经失去了设置这些更改动画的能力。我查看了视图文档,以找到一种方法来设置这些约束更改的动画,但似乎找不到一种合适的方法来覆盖动画块中的调用
[self layoutifneed]
。即使在这种情况下,我觉得应该没有必要这样称呼它,因为一个布局将被执行,我觉得我不应该强迫它当时和那里
有人知道如何使这些约束更改动画化吗?您需要调用
[self-updateConstraintsIfNeeded]
。所以你的代码看起来像这样
[self updateConstraintsIfNeeded];
[UIView animateWithDuration:0.25 animations:^{
[self layoutIfNeeded];
}];