Animating a Jumping Pin

I recently read a fantastic article on how to animate a really slick dribble animation using Android’s AnimatedVectorDrawable. This inspired me to see how it might look when implemented using Core Animation.

It’s been a while since I’ve done much Core Animation, so it was an excellent exercise for me to dust of my rusty skills and see how various techniques play together.

The animation above is comprised of several distinct components:

The jumping pin

The scrolling dots

Fading in/out

This article describes the techniques I used to implement each of these.

Jumping pin

The author (Nick Butcher) of the Android article did an excellent job of describing how the pin is drawn as a five SVG paths. Essentially, the animation can be modelled as a series of path interpolations.

In one of the gists he links to in his post, Nick has conveniently defined each of the constituent paths. For example, the SVG path for the first frame looks like:

I started manually converting this into a sequence of UIBezierPath commands, however that quickly turned frustrating and error prone. I discovered an awesome site by Mike Engel that can take an SVG path and output the equivalent UIBezierPath commands. For example, the output for the SVG path above is:

Note that we include pinPath1 at both the start and end of the values array. If we do not do this, then when the animation restarts, it will “snap” from the 5th position back to the 1st which looks jerky and horrible. So, instead we animate it back to the starting position which means the restart begins from the same position giving us the smooth transition we seek.

Scrolling Dots

Firstly, let’s deal with the line that sits underneath the dots. It can be represented using a simple CAShapeLayer line:

In terms of the dots themselves, the technique that the original article presented is quite clever in that it creates a single view with 5 dots evenly spaced horizontally, and once the 4th dot arrives at the original location of the 1st dot, the animation is reset and starts again.

As it turns out, we can actually achieve the same effect by creating a container view with just four dots evenly distributed at 25%, 50%, 75% and 100% respectively. By creating the containing view at twice the width of the main viewport, there will only ever be (at most) three dots on the main viewport at once.

Once each dot has been added to a containing layer, we can animate the containing layer from right to left.

Fading In and Out

The last thing to add is the fading in and out on the edges. I chose to implement this using a simple transparent→white gradient layer spanning from top-to-bottom and 50 points wide. The line and dots just slide underneath this layer.

Conclusion

Core Animation offers an amazing amount of functionality out of the box. Looking back, the two most challenging pieces of this exercise for me were:

Deciding on which type of animations to use and how to link them together. I did consider using some more sophisticated keyframe animations to tie the timings together, however, as it turns out, judicious use of the duration parameter meant that I didn’t need to go to the trouble of meticulous keyframe timings.

The animation of the dots to appear as one continuous stream of dots is trickier than it looks. The specific location of the dots within the frame is extremely important to this animation appearing smooth. An alternative implementation would be to animate the dots individually dots and use an animation group to control the individual delays.

I’d love to hear feedback on where I could do things better/more effectively. Please feel free to email me or contact via twitter.

An Xcode 8 playground containing the full source can be found on github.