A timer object that allows your application to synchronize its drawing to the refresh rate of the display.


class CADisplayLink : NSObject


Your application initializes a new display link, providing a target object and a selector to be called when the screen is updated. To synchronize your display loop with the display, your application adds it to a run loop using the add(to:forMode:) method.

Once the display link is associated with a run loop, the selector on the target is called when the screen’s contents need to be updated. The target can read the display link’s timestamp property to retrieve the time that the previous frame was displayed. For example, an application that displays movies might use the timestamp to calculate which video frame will be displayed next. An application that performs its own animations might use the timestamp to determine where and how displayed objects appear in the upcoming frame.

The duration property provides the amount of time between frames at the maximumFramesPerSecond. To calculate the actual frame duration, use targetTimestamp - timestamp. You can use this value in your application to calculate the frame rate of the display, the approximate time that the next frame will be displayed, and to adjust the drawing behavior so that the next frame is prepared in time to be displayed.

Your application can disable notifications by setting the isPaused property to true. Also, if your application cannot provide frames in the time provided, you may want to choose a slower frame rate. An application with a slower but consistent frame rate appears smoother to the user than an application that skips frames. You can define the number of frames per second by setting the preferredFramesPerSecond property.

When your application finishes with a display link, it should call invalidate() to remove it from all run loops and to disassociate it from the target.

Listing 1 shows how you can create a display link and add it to the current run loop. The display link invokes the step function, which simply prints the current timestamp, with each screen update.

Listing 1

Creating a display link

func createDisplayLink() {
    let displaylink = CADisplayLink(target: self,
                                    selector: #selector(step))
    displaylink.add(to: .current,
                    forMode: .defaultRunLoopMode)
func step(displaylink: CADisplayLink) {

CADisplayLink should not be subclassed.

Preferred and Actual Frame Rates

You can control a display link's frame rate, i.e. the number of times the specified selector of its target is called per second, by setting its preferredFramesPerSecond. However, the actual frames per second may differ from the preferred value you set: actual frame rates are always a factor of the maximum refresh rate of the device.

For example, if your device's maximum refresh rate is 60 frames per second (defined by maximumFramesPerSecond), actual frame rates include 15, 20, 30, and 60 frames per second. If you set a display link's preferred frame rate to a value higher than the maximum, the actual frame rate is the maximum.

Preferred frame rates than are not a divisor of the maximum frame rate are rounded to the nearest factor. For example, setting a preferred frame rate to either 26 or 35 frames per second on a device with a maximum refresh rate of 60 frames per second yields an actual frame rate of 30 times per second.

Listing 2 shows how you can calculate the actual frame rate by dividing 1 by your display link's timestamp subtracted from its targetTimestamp.

Listing 2

Calculating actual frame rate

// displaylink is a `CADisplayLink`

let actualFramesPerSecond = 1 / (displaylink.targetTimestamp - displaylink.timestamp)


Creating Instances

init(target: Any, selector: Selector)

Returns a new display link.

Scheduling the Display Link to Send Notifications

func add(to: RunLoop, forMode: RunLoop.Mode)

Registers the display link with a run loop.

func remove(from: RunLoop, forMode: RunLoop.Mode)

Removes the display link from the run loop for the given mode.

func invalidate()

Removes the display link from all run loop modes.

Configuring the Display Link

var duration: CFTimeInterval

The time interval between screen refresh updates.

var preferredFramesPerSecond: Int

The preferred frame rate for the display link callback.

var frameInterval: Int

The number of frames that must pass before the display link notifies the target again.

var isPaused: Bool

A Boolean value that states whether the display link’s notifications to the target are suspended.

var timestamp: CFTimeInterval

The time value associated with the last frame that was displayed.

var targetTimestamp: CFTimeInterval

The time value associated with the next frame that was displayed.


Inherits From

Conforms To

See Also

Animation Timing

func CACurrentMediaTime() -> CFTimeInterval

Returns the current absolute time, in seconds.

class CAMediaTimingFunction

A function that defines the pacing of an animation as a timing curve.

protocol CAMediaTiming

Methods that model a hierarchical timing system, allowing objects to map time between their parent and local time.