Core animation programming guide

Source: Internet
Author: User
About core AnimationCore animation is the infrastructure for graphics rendering and animation on iOS and OS X platforms. Core animation allows animation views and other visual elements. Core animation completes most of the frame painting work required for animation. You only need to configure a small number of animation parameters (such as the start point and end point) to start core animation. Core animation handed over most of the actual drawing tasks to the graphic hardware for processing, and the graphic hardware accelerates the graphic rendering speed. This automated graphic acceleration enables the animation to have a higher Frame Rate and smoother, but it does not increase the CPU burden and will affect the running speed of your application. If you are developing an iOS application, you have used core animation. If you are developing an OS X application, you only need to make a small effort to use the powerful features of core animation. For example, core animation is located at the bottom layer of appkit and uikit. It is tightly integrated into the cocoa and cocoa touch view workflows. Although tightly integrated, core animation also has interfaces for extended functions, which are exposed to the application view. Using these interfaces allows you to control animations in an application in a finer granularity. IntroductionYou may never need to use core animation directly, but when you use core animation, you should know that core animation is part of your app infrastructure. Core animation manages your application contentCore animation itself is not a drawing system. It is only a basic component responsible for integrating and manipulating application content in hardware. The core of core animation is the layer object, which is used to manage and manipulate your application content. Layers place the captured content in a bitmap. The graphic hardware can easily manipulate your bitmap. In most applications, layers are used as a way to manage View content, but you can also create standard layers, depending on your own needs. An animation is generated when you change the layer attribute.Most animations you create using core animation contain changes to layer attributes. Like a view, a layer object also has a border rectangle, coordinate origin, size, opacity, transform matrix, and many other visualized attributes (such as backgroundcolor ). When the values of most of these attributes change, an implicit animation is triggered. Implicit animation is an animation from the animation of the old property value to the new property value. If you need full control over animation behavior, you can consider using explicit animation attributes.   Organized LayersThere is a parent-child relationship between organized layers. The organization of the layer affects the visible content of the layer. Visible content management is similar to view management. The view level corresponding to the level image of the layer set attached to the view. You can add independent layers to expand the visible content of your view. Use an action object to change the default behavior of a LayerYou can use the action object to perform implicit layer animation. An action object is a common object that implements a predefined interface. Core animation uses an action object to implement a regular default animation set associated with a layer. You can create an action object of your own to implement a Custom Animation or other action types. Then you assign an action object to a certain attribute of the layer. When the attribute changes, core animation retrieves your action object and tells the action object to perform the corresponding action. How to use this documentThis document is intended for developers who require more comprehensive control of animation effects or intend to use layers to improve drawing performance. This document also describes how to combine layers and views in iOS and OS X. Layers and views are different on iOS and OS X platforms. Understanding these differences is critical to creating efficient animations. PrerequisitesYou should be familiar with the view architecture of your target platform and how to create view-based animations. If you have not reached this stage, read the following two documents: for iOS application development, you should understand the view architecture description in the IOS view programming guide. For OS X application development, you should understand the view architecture description in view programming guide. Core animation BasicsCore animation provides a general system for animation views and other visual elements. Core animation is not a substitute for views. Instead, it is a technology integrated with views. Because bitmap can be directly controlled by graphic hardware, By caching the View content in place of the graph, this technology can achieve better performance and Support Animation View content. In addition to caching the View content, core animation also defines how to specify any visible content, then integrate the content with the view, and finally the animation view and other visual elements. Use core animation to make changes to views and visual objects animated. Most changes are related to changes to the attributes of the visual object. For example, core animation allows the position, size, or transparency of a view to be animated. When you change the values of these attributes, the core animation is animated between the current attribute value and the latest attribute value. Generally, you do not need to replace the View content 60 times per second like a cartoon. Instead, you should use the mobile view provided by core animation, fade-in and fade-out view, apply any changes to the view, and modify other visual attributes of the view to complete the animation. Layers are the foundation of drawing and animationA layer object is a two-dimensional plane organized in a 3D space. It is the core component that uses core animation to perform any operation. Like a view, the management information of a layer includes the geometric structure, content, and visual attributes. Unlike a view, a layer does not define its own appearance, and only manages the status information of the surrounding bitmap. A Bitmap can be a drawing result of a view or an image. Therefore, the main layers are considered model objects because they are mainly used to manage data. This concept must be remembered because it affects animation behavior. Layer-Based Drawing ModelMost layers do not actually draw. Instead, layers capture the content of the application and cache them in place. Bitmap is also called a backing store ). When you change the attribute value of a layer, all you do is change the state information associated with the layer object. When your changes trigger an animation, core animation passes the bitmap of the layer and the state of the layer to the graphic processing hardware. The graphic processor uses the obtained information to render the bitmap. As shown in Figure 1-1, the bitmap operated by the graphic processing hardware has a higher animation effect than the graphic processing software. Because static bitmaps are manipulated, layer-based plotting is significantly different from view-based plotting. For view-based plotting, changes to the view often trigger the call of the view's drawrect: Method to repaint the View content. However, the cost of this method is relatively high because it is the operation of the CPU on the main thread. Core animation avoids this overhead by using graphics hardware to manipulate cached bitmaps as much as possible to achieve the same or similar effect. Although core animation tries its best to use cached content, it must also provide initial content and update it from time to time. Layer-based animationThe layer data and state information are separated from the visual presentation of the layer content. This separation gives core animation intervention and the opportunity to animation the old property value to the new property value. For example, changing the position attribute of a layer causes core animation to move the layer from its current position to a new position. Similar changes to other attributes will lead to appropriate animation effects. Figure 1-2 shows some animation types that can be executed on layers. Figure 1-2 shows the animation types that can be executed on layers. During the animation, core animation uses hardware to help you draw each frame. You only need to specify the start point and end point of the animation, and the rest is done by core animation. If needed, you can also specify custom time information and Animation Parameters. If not specified, core animation provides the appropriate default value.   A layer object defines its own geometric structure.One of the tasks of layers is to manage the visible ry of their own content. The visible ry contains the content boundary of the layer, the position on the screen, whether the layer has been rotated, scaled, or applied a certain transformation. Similar to a view, a layer has a frame and bound attribute. You can use these two attributes to locate the layer and its content. A layer also has some attributes that are not available to a view, such as anchor attributes. Any transformation operation is performed around this point (it can be understood as a graph pin on the layer ). The method of specifying some geometric concepts of a layer is different from that of the View information.   Two types of coordinate systems are used for layersLayers use the point-based coordinate system and the layout of the content specified by the Unit coordinate system. The selection of coordinate systems depends on the type of information to be transmitted. When the specified value is directly mapped to the screen or relative to the coordinates of other layers, the point-based coordinate system is used for the position attribute of the ratio layer. When the specified value is relative to some other values and is not associated with screen coordinates, the Unit coordinates are used. The anchorpoint attribute specifies a point relative to the layer boundary. The anchorpoint attribute can be changed. Point-based coordinates are most commonly used to specify the size and position of a layer. You can set the size and position of the layer through the bounds and postion attributes of the layer. Bound defines the coordinate system of the layer and the size of the layer on the screen. The position attribute defines the position of the layer relative to the parent coordinate system. Although a layer has a frame attribute, this attribute is actually derived from the bounds and position attributes and rarely used. The bounds and frame rectangles of a layer always match the default orientation of the underlying platform. Figure 1-3 shows the default direction of the boundary rectangle on the iOS and OS X platforms. In iOS, by default, the origin of the boundary rectangle is in the upper left corner of the layer. The origin of the upper boundary rectangle of OS X is in the lower left corner. If you share the core animation code between iOS and OS X apps, you must treat this situation differently. Figure 1-3 default layer geometric structure of the IOS and OS X platforms notice that the position attribute in Figure 1-3 is located in the middle of the layer. For changes to the position attribute, refer to the anchorpoint attribute of the layer. An anchor is one of the attributes of a Unit coordinate system. In core animation, attribute values represented by Unit coordinates may change due to layer size changes. You can regard the Unit coordinates as the percentage of the total number. In the Unit coordinate system space, the value range of each subject is between 0.0 and 1.0. For example, along the X axis, the left side is 0.0, and the right side is. Along the Y axis, the difference in the direction of the Unit coordinate value depends on the specific platform. 1-4. Figure 1-4 default Unit coordinate system on iOS and OS x platforms Note: The geometryflipped attribute does not appear until OS X 10.8. This attribute can change the Y coordinate direction of the default layer. This attribute is required to adjust the direction of the layer when the flip transform is called. If the parent view uses a flip transform, its child View content (and its corresponding layers) is often reversed. In this case, setting the geometryflipped attribute of the Child layer to yes is the easiest way to solve this problem. In OS X 10.8 and later versions, appkit is responsible for managing this attribute and you should not change it. Geometryflipped is not recommended for iOS apps. All coordinate values, whether point or Unit coordinate, are specified with floating point numbers. Floating Point Numbers allow you to specify more accurately the points that may fall between integer coordinate values. In particular, printing or drawing to the Retina screen makes floating point values very convenient. The floating point value allows you to ignore the resolution of the underlying device, but you only need to specify the precision of the value. Operations on the impact of anchor on Geometric StructuresThe geometric operation of a layer is relative to the anchorpoint of the layer. The anchorpoint attribute can access the anchorpoint value of the layer. When the postion and transform attribute values of a layer are changed, the effect of the anchor is obvious. The position attribute is specified relative to the anchor of the layer. In addition, any change to the layer shadow is relative to the anchor. Figure 1-5 illustrates the effect of changing the anchor on the layer position attribute. Although a layer does not move within its parent boundary, moving the anchor from the center point to the origin of the layer boundary changes the position attribute value. Figure 1-5 How the anchor influences the position attribute of the layer Figure 1-6 shows how the anchor changes apply to the layer. When you apply a rotation transformation to the layer, the layer rotates around the center point. By default, the anchor is set at the center of the layer. Generally, creating such rotation behaviors is exactly what you expect. However, if you change the value of the anchor, the rotation results also change. Figure 1-6 how the anchor influences the layer Transformation Layers can be operated in 3D spaceA layer has two transformation matrices for controlling layers and content, transform and sublayertransform attributes. The transform attribute of calayer is used to specify the transform applied to the layer and its embedded child layer. This attribute is usually used when you want to change the layer itself. For example, you may use this attribute to scale or rotate a layer, or temporarily change the position of a layer. The sublayertransform attribute defines the transform applied only on the child layer and adds the perspective effect to the scene content. The transformation behavior is determined by multiple coordinates. These coordinates are obtained through a digital matrix. The obtained coordinate result is the version after the original point is transformed. Because the core animation value can be specified in a 3D space, each coordinate point has four values, which need to be multiplied by a matrix of 4*4. 1-7. In core animation, the transformations in the graph are represented by the catransform3d type. Fortunately, you do not need to directly change the Domain value in this structure to perform the standard transformation. Core animation provides a comprehensive function set for creating scaling, translation, rotation matrices, and matrix comparisons. In addition to using functions to manipulate transformations. Core animation extends the key-value encoding to allow you to use the Key Path to change a transformation operation. Figure 1-7 use matrix mathematics to convert a coordinate Diagram 1-8 shows some common moment-changing configurations. Any coordinate multiplied by the unit matrix returns the exact same coordinate. How coordinates are changed depends entirely on the matrix factor you have changed. For example, to translate only on the X axis, you only need to apply a non-zero value to the Tx factor of the transformation matrix and set the Ty and TZ values to 0. For rotation, apply the sine and Cosine values of a rotation angle. Figure 1-8 matrix configuration of common Transformations Different layer trees reflect different animation statesThe app that uses core animation has three layers. Each set of layer objects plays different roles in rendering app content. The objects in the model layer tree (or "layer Tree" for short) are used most often. The object in this tree is the model object, which stores the target values of all animations. Whenever you change the attribute value of a layer, you always use a model object. The objects in the rendering tree contain the instantaneous values of all running animations. The layer tree object contains the animation target value, and the object in the rendering tree represents the current value of the animation displayed on the screen. You should not change the objects in this tree. Instead, you use these objects to read the values of the current animation, which may be used to create new animations starting with these values. Execute the actual animation on the objects in the rendering tree, and the core animation is not made public. Each layer object set is organized in a hierarchy, similar to a view in the app. In fact, for an app with all Views supporting the layer function, the initial structure of each tree matches the layers. An app can add another layer object, so the layer is not associated with the view. You can insert a layer object to a specified view level as required. You may choose to add a layer object instead of a view to optimize the performance of the app content, because the layer overhead is lower than the view. Figure 1-9 shows the layer breakdown chart that appears in a simple iOS app. In the example window, a content view contains a button view and two independent layer objects. Each view has a corresponding layer object that forms a layer. Figure 1-9 each object of the layer associated with the window exists in the rendering tree and the rendering tree. 1-10. As mentioned earlier, an app interacts with objects in the layer tree, but may sometimes access objects in the rendering tree. Specifically, accessing the presentationlayer attribute of an object in the layer tree will return an object corresponding to the object in the rendering tree. You may use this object to obtain the attribute value at a certain time point during animation execution. Figure 1-10 the layers tree in the window is important: only access the objects in the rendering tree when the animation is running. When the animation is in progress, the rendering tree contains the value of the moment when the layer is displayed on the screen. Unlike the layer tree, the layer tree always represents the final target value. A layer is not a substitute for a view. Therefore, you cannot create a visual interface based on a single layer object. A layer is the infrastructure of a view. Specifically, layers make drawing and animation of views easier and more efficient, and maintain a high frame rate during drawing and animation. However, layers cannot do many things. Layers cannot process events, draw content, especially in the response chain, or do something else. Therefore, each app must have one or more views to process such interactions. In iOS, each view has a corresponding layer object. However, in OS X, you must decide which view has a layer. In OS X 10.8 and earlier versions, adding layers to all views may make sense. However, you do not have to do this. You can still close the layer to prevent unnecessary and irrational overhead. Layers reduce program memory overhead to some extent, and these advantages of layers far exceed its disadvantages. Therefore, it is best to test the performance of your app before disabling Layer Support. When your view supports layers, this view is called a layer-backed view. For the layer-backed view, the system is responsible for creating the layer object at the underlying layer and maintaining the synchronization with the view. Therefore, IOS views Support layers, and most views in OS X also support layers. However, in OS X, you can also create a layer-hosting view, which is provided by you. NOTE: For the layer-backed view, we recommend that you operate only the view instead of the layer. In iOS, the image is only a thin package of the layer object, so any operations on the layer usually work normally. However, manipulating layers on iOS and OS X platforms rather than layers may not achieve the expected results. This document will try to point out these traps and provide a way for you to work with them. In addition to view-related layers, you can also create independent layer objects. You can embed these independent layer objects into any other layer objects, including view-related layers. You generally use a layer object as part of a specific optimization method. For example, if you want to use the same image in multiple places, you can load the image only once and associate the image with multiple independent layer objects, add these layer objects to the layer tree. Each layer only references the source image instead of creating a copy of the image in the memory.

Core animation programming guide

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.