Ioscalayer and Time model

Source: Internet
Author: User
Tags local time

Transfer from http://geeklu.com/2012/09/animation-in-ios/two. Calayer and time model

We all know that UIView is the view.uiview of MVC. The responsibility lies in the display of interfaces and the handling of interface events. Each view has a layer behind it (accessed through View.layer), and the layer is used for the interface display. Calayer belongs to the Quartzcore framework, which is very important, but not as well understood as imagined. The layer that we typically manipulate to display is the role of the data model in the concept of the core animation. It does not directly do rendering work. About layer, which was previously analyzed from the point of view of the coordinate system, this time it is focused on its temporal systems.

1.Layer Rendering Architecture

Layer also has a hierarchical tree structure like view, called the layer tree, which is created directly or obtained through UIView (View.layer) for the display of the layer tree, called the Model tree, There are also two copies of the layer tree behind the model tree, one for the Presentation tree and one for the render tree. The rendering tree can be obtained from the layer.presentationlayer of the normal layer (which is actually the model tree), while the model tree can be obtained through the Modellayer attribute (detail document). The properties of the model tree become new values as they are modified. This is the part that can be manipulated directly by the code, and the property values of the rendering tree are consistent with those seen on the interface during the animation. While the render tree is private and you cannot access it, the render tree renders the data of the rendering tree, in order not to block the main thread, the rendering process is performed in a separate process or thread, So you'll find that animation animations don't block the main thread.

2. Transaction Management

Calayer's (animatable) attributes, called animatable Properties, have a list of the details, listing all Calayer animatable properties. If a layer object has a corresponding view, the layer is called a root layer, and the non-root layer is typically created directly from Calayer or its subclasses. The following sublayer is a typical non-root layer, It does not have a corresponding view object associated with it.

    Sublayer=[[CalayerAlloc] init; sublayer. Frame = cgrectmake (0 0300300sublayer. BackgroundColor = [[uicolor redcolor ] cgcolor; [self. View. Layer addsublayer:sublayer];        

All non-root layers have implicit animations when setting animatable properties, and the default duration is 0.25 seconds.

    subLayer.position = CGPointMake(300,400);

Like the above code, the next time a runloop start is not directly to the sublayer position into (300,400), but a moving animation to complete the transition.

Any setting of the Animatable property of a layer should belong to a CA transaction (catransaction), and the purpose of the transaction is to ensure that multiple animatable attributes are changed at the same time, whether it is between the same layer or a different layer. Catransaction is also divided into two categories, explicit and implicit, when a animatable attribute is set in a Runloop, a CA transaction is automatically created if no transaction is found, and the transaction is automatically commit when the next runloop of the thread starts. , you must use an explicit transaction if you set the Animatable property of the layer in a place where there is no runloop.

The use of an explicit transaction is as follows:

[CATransaction begin];... [CATransaction commit];

Transactions can be nested. When a transaction is nested, the entire animation starts only when the outermost transaction commits.

You can set a transaction-level animation property through Catransaction, overriding the related properties of an implicit animation, such as a duration that overrides an implicit animation, Timingfunction. If the explicit animation is not set duration or timingfunction, then these parameters of the CA transaction settings will also work on this explicit animation.

You can also set Completionblock, Completionblock is called when all the animation for the current catransaction finishes.

3. Time System

The

Calayer implements the Camediatiming protocol. Calayer implements a hierarchical time system through the Camediatiming protocol. In addition to Calayer,caanimation also adopted this protocol, the time system used to implement animation.  
in the CA, there is a absolute The time (absolute time) concept can be obtained by Cacurrentmediatime (), in fact, this absolute time is the value of converting mach_absolute_time () into seconds. This is about the uptime of the system, Cacurrentmediatime () will be reset after the system restarts.   the
has the same relative coordinates as the coordinates, and different objects that implement the hierarchical relationship of the Camediatiming protocol also have relative time, which often requires time conversion. Calayer provides two time-conversion methods:

- (CFTimeInterval)convertTime:(CFTimeInterval)t fromLayer:(CALayer *)l;- (CFTimeInterval)convertTime:(CFTimeInterval)t toLayer:(CALayer *)l;

Now focus on several important attributes of the camediatiming protocol.

BeginTime

Whether it is a layer or an animation, there is a concept of timeline timeline, and their begintime is the start time relative to the parent object. Although not indicated in Apple's documentation, it can be found through code testing that all Calayer layers have a consistent timeline by default, their begintime are 0, and absolute time is the size of the absolute time that is converted to the current layer. So for a layer, Although they are created successively, their timelines are consistent (as long as they are not actively modifying the begintime of a layer), so we can imagine that all layers default to the timing of their timelines starting after the system restarts.

But the time line of the animation is different, when an animation is created, added to a layer, it will be copied to join the current layer, when the CA transaction is committed, if the begintime of the animation in the layer is 0, The begintime is set to the current time of the current layer, which causes the animation to start immediately. If you want an animation that is directly added to the layer to be executed later, you can manually set the animation's begintime, but note that this begintime needs to be Cacurrentmediatime () + delayed number of seconds, because BeginTime refers to a time on the timeline of its parent object, at which point the parent object of the animation is the layer that is added, and the current time of the layer is actually [layer convertTime: Cacurrentmediatime () Fromlayer:nil], in fact, is equal to Cacurrentmediatime (), then the time line on this layer delay a certain number of seconds to get the above results.

Timeoffset

This timeoffset may be one of the more difficult to understand in these properties, and the official documentation is not very clear. Local time is also divided into two types: active local time, which is the basic local time.
The Timeoffset is the offset of the active local time.
You think of an animation as a ring, timeoffset change is actually the beginning of the animation in the ring, such as a duration for 5 seconds of animation, the Timeoffset is set to 2 (or 7, modulo 5 is 2), then the animation runs from the original 2 seconds to 5 seconds, Then 0 seconds to 2 seconds to complete the animation.

Speed

The Speed property is used to set the time flow of the current object relative to the time flow of the parent object, such as an animation begintime is 0, but the velocity is 2, then this animation is equivalent to 1 seconds of the parent object in the time stream of 2 seconds. The larger the speed, the faster the time goes, the faster the animation. For example, a layer of 2 for all of its fathers was 1, and it had a sublayer,speed of 2, Then a 8-second animation runs in this sublayer in just 2 seconds (8/(2 * 2)). So speed has an overlay effect.

Fillmode

Fillmode's role is to determine the behavior of the current object over a non-active time period. For example, before the animation starts, after the animation ends. If it is an animated caanimation, you need to set its removedoncompletion to No, otherwise fillmode will not work. Here's what each fillmode means.
kcafillmoderemoved This is the default value, that is, when the animation starts and after the end of the animation, the animation has no effect on the layer, the end of the animation, the layer will revert to the previous state
Kcafillmodeforwards When the animation is finished, the layer will remain the last state of the animation
kcafillmodebackwards This is relative to Kcafillmodeforwards, just before the animation starts, you just add the animation to a layer, The layer immediately enters the initial state of the animation and waits for the animation to begin. You can set the test code so that an animation is added to a layer with a delay of 5 seconds. And then you'll find that when the animation doesn't start, as long as the animation is added Layer,layer will be in the initial state of animation
Kcafillmodeboth Understand the above two, this is very good understanding, this is actually the above two synthesis. Before the animation joins, the layer is in the initial state of the animation, and the layer retains the last state of the animation after the animation is finished.

Some of the other parameters are relatively easy to understand.

Practical application

See Apple official QA1673 How to pause the animation of a layer tree

12345678910111213141516 -(void)pauseLayer:(CALayer*)layer{    CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];    layer.speed = 0.0;    layer.timeOffset = pausedTime;} -(void)resumeLayer:(CALayer*)layer{    CFTimeInterval pausedTime = [layer timeOffset];    layer.speed = 1.0;    layer.timeOffset = 0.0;    layer.beginTime = 0.0;    CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;    layer.beginTime = timeSincePause;}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.