Mobile Development 12 min read

Understanding CADisplayLink, RunLoop, and CoreAnimation in iOS Rendering

This article explains how CADisplayLink registers VSync signals via a mach port, how RunLoop sources and observers process those signals, the impact of adding multiple display links, and the role of CATransaction in committing layer updates and driving iOS UI animation.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Understanding CADisplayLink, RunLoop, and CoreAnimation in iOS Rendering

The POP library uses CADisplayLink to register VSync signals; by printing the trigger stack we see a source1 added to the RunLoop, which receives a mach message every 16.7 ms to activate the app’s RunLoop and sense VSync.

Adding multiple CADisplayLink instances to the same RunLoop is possible, but if the selector method is identical the VSync message will invoke the selector twice, because each call to [CADisplayLink displayLinkWithTarget:selector:] creates a new DisplayLinkItem that is registered as a separate source.

CADispalyLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawSomething)];
[link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
CADispalyLink *link2 = [CADisplayLink displayLinkWithTarget:self selector:@selector(drawSomething2)];
[link2 addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

The RunLoop contains several modes (e.g., UITrackingRunLoopMode , kCFRunLoopDefaultMode ) each with its own set of sources, timers, and observers. A typical RunLoop includes an AutoreleasePool observer, UIKit gesture/event sources, and a QuartzCore CATransaction observer that triggers before waiting.

When the RunLoop finishes a cycle, CATransaction::Commit is called, which performs three main actions: it calls layoutIfNeeded on the layer tree (propagating to layoutSubviews ), communicates with the render server via mach messages, and processes animations.

During commitIfNeeded , CoreAnimation creates render objects from layer properties, possibly using IOSurfaces, and finally calls [CALayer _didCommitLayer:] . This ensures that all UI updates are committed before the thread returns to kernel mode.

RunLoop wake‑up can be triggered by GCD (e.g., dispatch_async , dispatch_after ), network callbacks, or user interactions (touches, hardware buttons). The article also briefly covers 2D and 3D animation techniques, image morphing, and shape blending, linking them to CoreAnimation’s focus on 2D animation.

mobile developmentanimationiOSRunLoopCoreAnimationCADisplayLink
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.