Imagine a tall, thin look, we will say that it looks like a ladder.
It is 100 high. We animate it so that it is the full height of the screen. So, in the pseudo code ..
func expandLadder() { view.layoutIfNeeded() UIView.animate(withDuration: 4 ... ladderTopToTopOfScreenConstraint = 0 ladderBottomToBottomOfScreenConstraint = 0 view.layoutIfNeeded() }
No problems.
Anyway. We draw a ladder in the Subviews layout. (Our staircase is just a thick black line.)
@IBDesignable class LadderView: UIView { override func layoutSubviews() { super.layoutSubviews() setup() } func setup() { heightNow = frame size height print("I've recalculated the height!") shapeLayer ... topPoint = (0, 0) bottomPoint = (0, heightNow) p.addLines(between: [topPoint, bottomPoint]) shapeLayer!.path = p shapeLayer add a CABasicAnimation or whatever layer.addSublayer(shapeLayer!) }
No problems.
Thus, we simply create a line p that fills the height of the "heightNow" of the view.
The problem, of course, when you do this ...
func expandLadder() { view.layoutIfNeeded() UIView.animate(withDuration: 4 ... ladderToTopOfScreenConstraint = 0 ladderBottomOfScreenConstraint = 0 view.layoutIfNeeded() }
... when you do this, LayoutSubviews (or setBounds, or draw # rect, or whatever you can think of) is called only before and after the UIView.animate animation. This is a real pain.
In this example, the "actual" shapeLayer ladder will go to the following values before you animate the size of the LIE view: "I recalculated the height!" called only once before and once after animating UIView.animate. Assuming you don’t crop the subviews, you will see a “wrong, upcoming” size before each animation of length L.)
What's the solution?
By the way, of course, you can do this by using draw # rect and animating yourself, i.e. with CADisplayLink (as mentioned by Josh). So good. It is incredibly simple to draw shapes, simply using a layer / path: my question here is how to make, in the example, the code in setup () run at each stage of the animation of the UIView constraints of the type in question.