2
votes

I have a UIView which contains subviews. The subviews are animated with constraints changes and layoutIfNeeded() function :

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)

But when I do this, it also animate the contraints changes of the parent. Something like this :

self.Height == containerView.Height

How can I call layoutIfNeeded() to animate my subviews but not the parent ?

EDIT : The side effect : http://gph.is/2noI3w9

1
Try manually calling parent.layoutIfNeeded immediately before you layout self in the animation block. This will force the parent to layout without animations before self lays out with animations - Matthew Hallatt
When I do parent.layoutIfNeeded() it animates all its subviews - Will
How about if you call it before you update the constraints? - Matthew Hallatt
Doesn't work... :( I think it's because the constraint that I don't want to animate is dynamic and depending of the subviews constraints - Will
If the constraint of the parent is dependant on the constraints of the children, why do you not want them to animate together? It seems to me like the effect would be jarring if one animates but the other doesn't. - Matthew Hallatt

1 Answers

1
votes

You can call this inside of your UIView.animate

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

Instead of

self.layoutIfNeeded()