How to perform animations in Metal using a CADisplayLink

Oscar de la Hera Gomez
Three flowers that represent Swift, Metal and Xcode with the text "Animations using a CADisplayLink" beneath them.

A step by step tutorial on animating the origin of a shape in Metal using a CADisplayLink.

We recommend that you clone our Open Source Swift Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/metal/cadisplaylink-animation branch.

git clone git@github.com:delasign/swift-starter-project.git

This tutorial first walks you through how to the project to draw multiple shapes from multiple shaders, with shared functionality (Step One) and then demonstrates how to animate the origin of a shape using a CADisplayLink.

A CADisplayLink is a timer object that allows your app to synchronize its drawing to the refresh rate of the display.

Although you can change uniforms/arguments at any moment and wait till the next Metal render loop, a CADisplayLink is the recommended approach for updating metal shader uniforms/arguments to match how the device render loop.

Tutorial

Step One: Setup the Project

A screenshot of an iPhone showing two shapes drawn in metal. One is filled and the other is outlined with a stroke that we set in code. This proves the functionality works.

Follow the tutorials below to learn how to create a polygons, with a color fill or with a variable line width through shared functionality.

A screenshot of Xcode showing the MetalUIView.swift file. It has highlighted the variables that we have declared and that we have called the setupCADisplayLink function. The setupCADisplayLink function is created in Step Four.

In the MetalUIView.swift file declare the relevant variables and call the setupCADisplayLink() function which will be created in Step Four.

Step Three: Update the Metal Delegate

A screenshot of Xcode showing the MetalUIView+MetalDelegate.swift file. We have highlighted how our code makes use of a variable (the origin) that we are going to animate as part of this tutorial.

In the MetalUIView+MetalDelegate.swift file, update the drawing function to make use of the animated variables that you created in Step Two.

Step Four: Create the Animation Functionality

A screenshot of Xcode showing the MetalUIView+Animation.swift file. The code is provided below.

Create a MetalUIView+Animation.swift file and paste in the code below.

If you are looking for more information on the code above:
  • The setupCADisplayLink() function sets up the CADisplayLink target, selector and determines the screens refresh rate.
  • The updateAnimationVariables() function gets called by the CADisplayLink every time the system updates the screen and allows you to synchronize animation with the screens refresh rate.

Please note that the refresh rate of the CADisplayLink may be 0, in which case it matches that of the screen. To learn more about this read the CADisplayLink documentation provided by Apple.

Step Five: Test

A screenshot of an iPhone showing that the shape has moved from its origin. This is meant to inform you that the shape now animates on the screen.

Run the code on a device and confirm that the animations work as expected.

Looking to learn more about things you can do with Swift, Metal and XCode ?

Search our blog to find educational content on learning how to use Swift, Metal and XCode.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details