Animation development in iOS (Overview), ios Overview
I. Introduction
When using animations in iOS development, you can setduration
,speed
,begintime
,offset
To set the animation duration, speed, start time, and start offset.
A simple example is used to describe the functions 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. The effect is as follows.
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];
After modification, the effect is as follows:
Ii. Hierarchical Time Structure
According to the document,CMediaTiming
The protocol constructs a hierarchical Time System and uses this hierarchical time system to coordinate the time of each layer and animation.
This Protocol isCAAnimation
AndCALayer
Compliance: each object that complies with the Protocol corresponds to onetime space
. Based on the relationship between objects, differenttime space
Hierarchical relationship. For example, if Layer A has A subLayer Btime space
Is corresponding to layer B.time space
Ofparent time space
. Everytime space
The time value is based onparent time space
Andbegintime
,speed
According to certain rules.
To facilitate understanding of the hierarchical time system, Let's first look at how the display position of the layer on the screen is determined, and then make an analogy.
Layer level. Determine the position of sublayer1 on the screen in three steps.
Similarly, to determine the time in sub1ayer1, three steps are required.
Compared with determining the location of a layer, determining the time is somewhat complicated. The following two points are proposed:
III,
active local time
This conversion aims to process the location of the object at the current level on the time line of the parent level, as well as the relationship between the time passing speed between the current level and the parent level.
Attributes related to this conversion include:beginTime
,speed
AndtimeOffset
Slaveparent time
Toactive local time
There is a formula for reference.
t = (tp - begin) * speed + offset
IV,
basic local time
This conversion aims to process repeat at the current level, and whether to perform reverse playback before replay.
For example, the current level is an animation (CAAnimation
ComplianceCAMediaTiming
Protocol ),duration
It is 1 s, after the first conversionactive local time
It is 5.5 s. IfrepeatCount
Is 10, after the second conversion,basic local time
It will be 0.5 s, so the animation shows half of the State.
V. Example
Based on this knowledge, you can explain the effect of setting parameters at the beginning of the article.
When an animation is added to a layertime space
Added to the hierarchical time system, corresponding to the layertime space
Sub-level.
6. More applications
UnderstandCAMediaTiming
After the protocol, you can achieve a lot of animation effects.
Apple has already given us the tools, so you can imagine what kind of products you can make.
Reference
Control animation time
Control animation time (Chinese Version above)
Time Warp in Animation