Time in iOS animation development, ios animation Development
Overview
In an animation, we specify the animation duration. For example
scaleAnimation.duration = self.config.appearDuration
So how is this time defined? Is it absolute time?
Hierarchical Time Structure
The display position of a layer on the screen is defined based on the position of the parent layer and its offset relative to the parent layer.
Similarly, each layer has its own time space. When calculating the local time, you need to calculate the local time according to the time of the parent layer and certain conversion rules.
This rule isCAMediaTiming
Protocol. EveryCALayer
AndCAAnimation
This Protocol is implemented.
Bytes
Bytes
Concept of time
CAMediaTiming
Example of protocol Time conversion formula
A simple example is used to describe the influence of each parameter. The animation is very simple. A Red Square moves from left to right. The animation lasts for 1 s and is not repeated.
About begintime
According to Formula
\[
T = max \ left \ {(t_p-begintime), 0 \ right \} * speed + offset
\]
How should we specify begintime here? If you want to delay the execution of an animation added to the layer by 5s, should you set begintime to 5 directly?
Because begintime is the time offset from the parent object (layer. Since the layer may have existed a long time ago, \ (t_p \) is a large value for animations. If you specify begintime as 5 s, \ (t \) will be a large value. The correct method is to set begintime to 5 s + after the layer is added to the parent layer, the time spent is called addtime.
animation.beginTime = addTime + delay;
How to Get addtime
addTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
Reference
Control animation time
Control animation time
Time Warp in Animation