Instance Property

targetTimestamp

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

Declaration

var targetTimestamp: CFTimeInterval { get }

Discussion

You can use the target timestamp to cancel or pause long running processes that may overrun the available time between frames in order to maintain a consistent frame rate.

The following code shows how you can create a display link and register it with a run loop. The step(displayLink:) function attempts to sum the square roots of all numbers up to max, but with each iteration checks the current time (CACurrentMediaTime()) against the targetTimestamp. If the time taken to complete the calculation is later than the target timestamp, the function breaks the loop:

func createDisplayLink() {
    let displayLink = CADisplayLink(target: self,
                                    selector: #selector(step))
    displayLink.add(to: .main,
                    forMode: .defaultRunLoopMode)
}
    
func step(displayLink: CADisplayLink) {
    var sqrtSum = 0.0
    for i in 0 ..< Int.max {
        sqrtSum += sqrt(Double(i))
        
        if (CACurrentMediaTime() >= displayLink.targetTimestamp) {
            print("break at i =", i)
            break
        }
    }
}

See Also

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.

Deprecated
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.