我有一个包含子视图的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

最佳答案

你可以在你的UIView.animate中调用它

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

而不是
self.layoutIfNeeded()

关于swift - 如何在Swift中不使用layoutIfNeeded()为特定的约束变化设置动画?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42768049/

10-09 17:18