如何使用Swift中的layoutIfNeeded()不为特定约束更改设置动画?

时间:2017-03-13 15:56:28

标签: swift animation autolayout nslayoutconstraint

我有一个包含子视图的UIView。子视图使用约束更改和layoutIfNeeded()函数进行动画处理:

for i in 1...cardViews.count - 1 {
    let currentCard = cardViews[i]
    let previousCard = cardViews[i-1]

    // Set new offset
    currentCard.topConstraint?.constant = previousCard.contentView.frame.height + configuration.expandedOffset
}

// To animate constraints's changes
UIView.animate(withDuration: TimeInterval(duration), delay: 0, options: [.curveEaseOut], animations: {
    self.layoutIfNeeded()
}, completion: nil)

但是当我这样做时,它也会动画父母的约束变化。像这样:

self.Height == containerView.Height

如何调用layoutIfNeeded()来动画我的子视图而不是父视图?

编辑:副作用: http://gph.is/2noI3w9

1 个答案:

答案 0 :(得分:1)

你可以在你的UIView.animate

中调用它
for i in 1...cardViews.count - 1 {
    let currentCard = cardViews[i]
    currentCard.layoutIfNeeded()
}

而不是

self.layoutIfNeeded()