targetTimestamp
The time interval that represents when the next frame displays.
Declaration
var targetTimestamp: CFTimeInterval { get }Mentioned in
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
}
}
}