is there a nice way to calculate the position of a path (CGPath or UIBezierPath) at a given time (from 0 to 1)?
Using CAShapeLayer for example, one can create an animated stroke end. I want to know the position of that stroke end at arbitrary times.
Thanks in advance, Adrian
Building on Matt's display link answer, you can track the position of the end point by creating a second "invisible" keyframe animation.
NOTES:
We start with 3 properties:
The
displayLinkwill allow us to run code every time the screen updates. ThepathLayerprovides the visuals, the one that we'll animate. ThetrackingLayerprovides an invisible layer that we'll use to track the position of thestrokeEndanimation on thepathLayer.We open our view controller like so:
With the following methods...
We first create the display link and add it to the run loop (as per Matt's code):
We then create the visible layer:
We then create an "invisible" (i.e. via a frame with no dimensions) layer to track:
We then create a method that grabs the position of the tracking layer:
... and the
startAnimatingmethod:This technique is pretty useful if you have paths you want to follow, but don't want to be bothered doing the math yourself.
Some of the valuable reasons for this are:
EDIT Here's a link to a github repo: https://github.com/C4Code/layerTrackPosition
Here's an image of my simulator: