简体   繁体   English

Swift-仅对特定约束进行动画处理

[英]Swift - Animate only specific constraints

I'm trying to animate constraints in my ViewController. 我正在尝试为ViewController中的约束设置动画。 In order to do that, I added this line to my code: I'm just trying to change the height of a UIView() (from 0 to 100) 为此,我在代码中添加了以下代码:我只是想更改UIView()的高度(从0到100)

barHeight.constant = CGFloat(100)
UIView.animate(withDuration: 2) {self.view.layoutIfNeeded()}

The problem is that, with that line, all of the constraints are animated, and it's not what I would like. 问题在于,在那条线中,所有约束都动画化了,这不是我想要的。

Do you know how I could animate only specific constraints and not others? 您知道我如何只能为特定约束设置动画,而不能为其他约束设置动画吗?

Call layoutSubviews before you edit the constraint you want to animate. 在编辑要设置动画的约束之前,请调用layoutSubviews。 This will cause any pending layout updates to be applied without any animation and then you can change the next constraint with animation. 这将导致在没有任何动画的情况下应用所有待处理的布局更新,然后您可以使用动画更改下一个约束。

Like so: 像这样:

 self.view.layoutSubviews()
 barHeight.constant = CGFloat(100)
 UIView.animate(withDuration: 2) {self.view.layoutIfNeeded()}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM