Core animation getting started

Source: Internet
Author: User

From: http://www.cocoachina.com/bbs/read.php? Tid-10972.html

Chapter 1 Introduction to core Animation

1. Introduction

Core animation is OBJC LanguageA set of encapsulated graphics rendering, projection, and animation libraries.
Core animation makes it easy to create a user interface by using the following methods:
1. High-Performance Synthesis Using simple programming methods
2. Use a layer object to create a complex user interface
3. lightweight data structure that allows hundreds of layers to be animated at the same time.
4. Independent from applicationsProgramThe main thread and the animation run in a separate thread.
5. Improved application performance. The application only needs to repaint the changed part (partial refresh ).
6. Flexible layout management mode.

2. Related Classes
With core animation, developers can create beautiful animation interfaces without using underlying APIs or OpenGL.
The core animation class is divided into the following:
1. layer of the displayed content ).
2. animation and time class (animation and timing classes ).
3. layout and constraint classes ).
4. Divide multiple layers into several atomic update execution classes.

Layer 2 (layrer)

Layer is the basis of core animation. A view (uiview) instance has a calayer instance that acts as a superlayer and all child layers added on this layer, the created layer structure is called a layer tree (the structure between layers is like a tree ). When painting a layer, all layers specify a coordinate relative to the parent layer, and draw from the last layer, just like drawing a view. Layer allows more complex conversions, such as rotation, skew, amplification, and reduction, and the content of the projection layer.
The layer also manages its associated animations and functions (action, which is not translated here. Please advise ). When you insert or delete a layer from a layer tree, an animation is triggered when you modify its attributes and application requests.

2.2 animation and time

implicit animation
many changes to the visual attributes of the layer can produce implicit animation effects, because these attributes are associated with an animation by default. By simply setting the visible property value, the layer will produce a gradient animation effect from the current value to the set value. For example, setting the hidden attribute of a layer to true triggers a gradually disappearing animation effect.
explicit animation
you can set the animation attributes or create an explicit animation effect. You can create an animation class and specify the expected animation effect. Explicit animations do not change the attributes of layers.
all core animation classes are inherited from the abstract class caanimation. Caanimation adopts the camediatiming protocol. Camediatiming specifies the animation duration, speed, and number of repetitions. Caanimation also adopts the caaction protocol, which specifies the standard way to start an animation when responding to an action triggered by a layer.

Core animations also provide other animation types:
Catransition. Catransition specifies the effect of content conversion on the entire layer. During the animation, it enables the layer to produce fade, push, and reveal animations. These commonly used conversion effects can be extended through the Core Drawing filter.
Core Drawing filter related Chinese link: http://www.apple.com.cn/developer/Documentation/GraphicsImaging/Conceptual/CoreImaging/index.html
Caanimation. Caanimation allows a large number of animation objects to be divided into several groups and can run simultaneously.
Capropertyanimation. Is a subclass of caanimation. It allows you to specify a key path for a layer during animation.
Cabasicanimation. This class provides simple interpolation for layer attributes. (I understand that, because an animation is to be generated, to enable the animation to be generated during the start and end periods, insert other values for the range value set for the layer attribute .)
Cakeyframeanimation. Cakeyframeanimation provides support for key frame animation. You can specify a key path for the attribute of the animation layer. The path can be an array that stores the values of each stage of the animation, and the number of animation times and time functions of the key frame. When an animation is running, each value is used in turn during interpolation.

2.3 layout management:
The cakeyframeanimation class is used to manage the layout of all child layers. Each instance encapsulated by the caconstraint class describes the Geometric relationship between child layers.

2.4 Execution Management
You can set the attributes of the animation layer to be modified as part of the execution. Catransaction is responsible for dividing many animations into several batches of atomic updates for display.

3. class hierarchy chart

 
 

 

Chapter 2 core animation rendering Architecture

The layer of the core object is very similar to the view of cocoa, but the biggest difference is that the layer is not directly rendered to the screen.
In the MVC design mode, the view is obviously a V object, while the layer is a m object. The layer encapsulates the layout, timing, and visual attributes and provides the content for display, but the display is not the responsibility of the layer.
Each visible layer is supported by two corresponding trees: One Presentation tree and one render tree.
Core animation rendering architecture diagram:

 

The layer tree contains the data model values of each layer (you can set the layer attributes of the animation ).
The presentation tree contains the layer attribute values displayed to the user when an animation occurs.
When rendering the entire layer, the render tree uses the attribute value of the presentation tree. The render tree is responsible for implementing compound operations. It does not depend on the main loop of the application and will be executed in an independent process or thread.

 

Chapter 3 coordinate and transformation of Layers

This section describes the components of layer coordinates, the relationships between elements, and how to convert them to produce complex results.

Layer-1 Coordinate System
Layer coordinates use the same coordinate system as the quanz image. By default, the origin (0.0, 0.0) increases in the lower left corner, right, and up. Note: This coordinate system is different from the coordinate system of the uiview instance. The origin of the uiview system increases from the upper left corner to the right and down. Each layer instance maintains its own coordinate system, which is relative to this coordinate system when locating all child layers and drawing.

Geometric coordinates of Layer 2
Geometric coordinate system diagram of layers:

2.1 Bounds
No one knows about this. skip this step.

2.2 Position
Cgpoint type, which specifies the position relative to the parent layer (superlayer), represented by the coordinate system of the parent layer.
2.3 Frame
When the frame of a layer is changed, the position and bounds of the layer are changed accordingly, but the layer does not have the frame attribute. It consists of the position, bounds, and anchorpoint of the layer (reference point) and transform.

2.4 anchor point reference point
Key attributes of today. Be sure to understand. This attribute is a point in the bounds range of the layer. The value ranges from 0.0 to 1.0. All conversions are calculated relative to this vertex.
Is the value of three anchor points:

Next, let's take a look at the relationship between frame, bounds, position, and anchorpoint.
For example:

 

In this figure, the default value of anchor point is (0.5, 0.5), which is equivalent to the center of the layer. If you set frame to (40.0, 60.0, 120.0, 80.0), the position value is automatically set to (100,100 ). We can also see that the Conversion Calculation of rotation and scaling is relative to the reference point.

The size of the frame is the same as that of Figure 3, but the anchor point is (0.0, 0.0 ):

 

2.5 cornerradius
When you want to draw, edit the child layer, and draw borders and shadows, you can specify the radius used by the layer to produce the rounded corner effect.

Layer-3 geometric coordinate transformation
As mentioned above, all of them lay the foundation for this section.
The catransform3d struct defines a structure for rotation, scaling, offset, skew, and pivot transformation.
. The prototype is as follows:
Struct catransform3d
{
Cgfloat M11, M12, M13, m14;
Cgfloat m21, m22, M23, M24;
Cgfloat m31, M32, M33, m34;
Cgfloat M41, M42, m43, m44;
};
Typedef struct catransform3d catransform3d;
You can assign the struct to the two attributes transform or sublayertransform of the layer to achieve coordinate transformation.

You can create and modify the struct using the following three methods:
1) Use the catransform3d function to obtain
2) directly modify the struct data member
3) Use Key-value encoding and Key Path (key paths)

3.1 conversion functions

3.1.1 translation, scaling, and rotation conversion functions (I can't understand how to perform Coordinate Conversion Calculation below. Linear Algebra is too abuse. I really regret that I didn't learn it very well at the beginning. ^ & ^. Tips ):

Example:
Reflectionlayer. Transform = catransform3dmakescale (1.0,-1.0, 1.0 );
This function reverses the Y coordinate of the layer to produce a reflection effect.
There is a catransform3dmakescale matrix conversion examples, you can look at, for understanding the matrix conversion is still very helpful, address: http://www.cocoachina.com/macdev/ca/2009/0611/33.html

The system also provides the catransform3dinvert function to reverse the matrix.

3.1.2 affine conversion functions:

3.1.3 test whether the function is equal: 

3.2 use key paths to modify the Conversion Function 

Example:
Mylayer. Transform. Rotation. x = 0;
[Mylayer setvalue: [nsnumber numberwithint: 0] forkeypath: @ "transform. Rotation. X"];

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.