Understanding CAMediaTiming Protocol and Hierarchical Time Structure in iOS Animations
The CAMediaTiming protocol defines a hierarchical time system for CALayer and CAAnimation objects, where each layer owns a nested time space whose beginTime, speed, and timeOffset combine to compute active and basic local times, enabling developers to pause, offset, speed up, repeat, or auto‑reverse animations with precise control.
In iOS development, animation timing can be controlled through the duration , speed , beginTime , and timeOffset properties defined by the CAMediaTiming protocol.
A simple example shows a red square moving from left to right over 1 s with no repeat. After modifying the timing properties, the animation runs twice as fast, starts after a delay, and begins from the centre of the view.
CFTimeInterval currentTime = CACurrentMediaTime();
CFTimeInterval currentTimeInLayer = [self.testLayer convertTime:currentTime fromLayer:nil];
CFTimeInterval addTime = currentTimeInLayer;
anim.beginTime = 0.3 + addTime;
[anim setTimeOffset:0.5];
[anim setSpeed:2];The CAMediaTiming protocol builds a hierarchical time system that coordinates the timing of every CALayer and CAAnimation object. Each object that conforms to the protocol owns a “time space”. Time spaces are nested: a sub‑layer’s time space is a child of its parent layer’s time space.
To determine the time in a sub‑layer, three steps are required, analogous to determining a layer’s screen position:
Obtain the parent layer’s time (window layer time).
Combine the parent time with the child layer’s beginTime and offset to compute the child’s time.
Apply the child’s beginTime , offset , and speed to obtain the final local time.
The conversion from parent time to active local time follows the formula:
t = (tp - begin) * speed + offsetActive local time is used to handle the relationship between a child’s time line and its parent’s time line, as well as the relative speed of time flow.
After the active local time is calculated, a second conversion produces the basic local time, which accounts for repeat behavior and optional auto‑reversing. For example, if an animation has a duration of 1 s, an active local time of 5.5 s, and a repeatCount of 10, the basic local time becomes 0.5 s, meaning the animation is displayed halfway through its current repeat.
Key properties involved in these conversions are:
beginTime : the time offset at which a child time space starts relative to its parent.
speed : the multiplier that determines how fast the child’s time advances compared to the parent.
timeOffset : an additional offset added to the child’s local time.
Understanding these concepts enables a variety of practical techniques:
Pause an animation by setting its layer’s speed to 0.
Create a door‑opening‑and‑closing effect by enabling autoreverses .
Stagger multiple animations on the same layer by assigning different beginTime values.
Manually scrub an animation’s progress by setting speed to 0 and adjusting timeOffset .
These capabilities are provided by Apple’s Core Animation framework; the only limit is the developer’s imagination.
References:
Controlling Animation Timing – http://ronnqvi.st/controlling-animation-timing/
Time Warp in Animation – http://wangling.me/2011/06/time-warp-in-animation.html#fn-1
Tencent Music Tech Team
Public account of Tencent Music's development team, focusing on technology sharing and communication.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.