CAReplicatorLayer: A 'For Loop' for Layers

CAReplicatorLayer is a 'for loop' for layers, creating complex patterns from a single source layer. Use it for loading spinners or radial bursts. The footgun: transformations are cumulative, so each copy is transformed relative to the previous one, not the…
WHY IT EXISTS To efficiently create and manage repeating visual elements with slight variations. It avoids the performance overhead and complexity of manually creating, positioning, and animating dozens or hundreds of individual layers, which would be cumbersome and slow.
THE MENTAL MODEL Think of CAReplicatorLayer as a layer factory with a transformation rule. You provide it one sublayer (the 'source'), and it stamps out multiple copies. Each copy is created by applying a transformation—like a rotation, translation, or color shift—to the previous copy.
HOW IT WORKS You create a CAReplicatorLayer and add a single sublayer to it. You then configure the replicator's properties. 'instanceCount' specifies how many copies to make. 'instanceTransform' is a CATransform3D that's applied to each successive copy. You can also set properties like 'instanceColor', 'instanceRedOffset', and 'instanceAlphaOffset' to change the appearance of each copy. If you animate the original sublayer, that animation is also replicated, and 'instanceDelay' adds a time offset between the start of each copy's animation.
WHEN TO USE IT Use it for visual effects composed of repeating geometric patterns. This includes circular loading indicators (replicating a dot with a rotation), audio VU meters (replicating a bar with a translation), or grid-like background patterns. It's perfect when you need many similar-looking layers that follow a predictable geometric or temporal sequence.
WHEN NOT TO USE IT Avoid CAReplicatorLayer when each element needs unique, independent behavior or a completely different appearance that can't be described by a simple cumulative transform. If your elements need to respond to individual user interactions or have complex, non-uniform animations, managing separate layers is more appropriate.
ONE CANONICAL EXAMPLE To make a classic circular loading spinner: create a CAReplicatorLayer and add a small circular CAShapeLayer as its sublayer, positioned slightly off-center. Set the replicator's 'instanceCount' to 12. Set its 'instanceTransform' to a rotation of 30 degrees (360 / 12). Finally, add a fade-out animation to the original circular layer and set an 'instanceDelay' on the replicator. The replicator will create 12 copies, each rotated 30 degrees from the last, and the animation will ripple through them, creating the spinning effect.
Read the original → developer.apple.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.