IOS learning-calayer and uiview

Source: Internet
Author: User

A calayer animation demo was created these two days,

Then I keep thinking about the relationship between calayer and uiview, the animation difference between calayer and uiview, and other issues.

Check your documents online and learn from them.

From: fairzy.

The biggest conceptual difference between the core animation layer and the cocoa view is that the layer is not drawn directly on the screen. Uiview is in the view layer in MVC mode, and core animation is actually in the model layer. It encapsulates geometric attributes, timing, and visible attributes when the content is displayed, however, it is not actually the responsibility of the layer.

The calayer layer tree is equivalent to the cocoa view inheritance tree. It has many things in common with uiview, but core animation does not provide a way to display it in the window. They must host in uiview, and uiview provides them with a response method. Therefore, uireponder is another major feature of uiview.

Animation and Layer

Time is everywhere in core animations. When you need an animation attribute for a layer, you need to consider the animation duration, how to change, whether to return the initial value, and the number of different values (basic animation and Key Frame Animation) in the attribute during the animation ). You can specify different animation attributes by setting the animation attribute values. However, these values cannot be assigned by the layer itself, but are assigned by cabasicanimation and cakeyframeanimation in the caanimation object or its subclass.

Again, in the MVC design pattern, a layer is a model object rather than a view object. The layer includes the following attributes: Position, outline, color, and so on. To put it simply, although an animation only needs to describe the animation itself, the animation attributes you set are also related to the layers you are doing the animation. You can call addanimation: forkeypath to add the animation object to the layer to trigger the animation. Similarly, you can call removeanimation: forkeypath to stop an animation in a layer. However, by default, after the animation is completed, the animation automatically performs the above actions.

1. uiview is the basis of interface elements in IOS systems. All interface elements are inherited from it. It is fully implemented by coreanimation (it does not seem like this in MAC ). The real plot is managed by a class called calayer (core animation Layer. Uiview itself is more like a calayer manager, accessing its properties related to plotting and coordinates, such as frame and bounds, in fact, they are all accessing the relevant attributes of calayer.

2. uiview has a layer attribute that can return its main calayer instance. uiview has a layerclass method that returns the class used by the main layer. The class of uiview can be reloaded, to make the uiview display with different calayer, for example, through

- (class) layerClass {  return ([CAEAGLLayer class]);}
3. The calayer of the uiview is similar to the sub-view tree structure of the uiview. You can also add a sub-layer to its layer to complete some special representations. For example, the following code graycover = [[calayer alloc] init]; graycover. backgroundcolor = [[uicolor blackcolor] colorwithalphacomponent: 0.2] cgcolor]; [self. layer addsublayer: graycover]; a black transparent film is added to the target view.

4. The layer tree of the uiview is inside the system, and three copies are maintained by the system.
First, the logic tree can be manipulated in the Code, such as modifying the layer attributes.
The second part is the animation tree, which is an intermediate layer where the system is modifying attributes and performing various rendering operations.
The third part is the display tree. The content of this tree is the content currently being displayed on the screen.
The logical structure of these three operators is the same, with only their respective properties.

5. animation operation
In addition to the main layer of the uiview (I think this is the case), the system will automatically animated the changes to its sublayer, that is, the attributes of the sublayer, the animation duration has a default time, which is about 0.5 seconds. During the animation time, the system automatically determines which attributes have been changed, automatically performs animation Interpolation on the changed attributes, generates intermediate frames, and then displays them continuously to produce the animation effect.

6. Coordinate System
The coordinate system of calayer is a little different from that of uiview. It has an attribute called anchorpoint, which uses the cgpoint structure, but the value range is 0 ~ 1, that is, set according to the proportion. This point is the coordinate origin of various graphic transformations. It also changes the position of the layer. Its default value is {0.5, 0.5}, that is, in the center of the layer.
A layer. anchorpoint = cgpointmake (0.f, 0.f );
If this is set, the upper left corner of the layer will be moved to the original center,
Just add this sentence.
A layer. Position = cgpointmake (0.f, 0.f );
Last, you can set the corner display (cornerradius) or shadowcolor for the layer ). However, if a layer in the layer tree has a rounded corner, the shadow effect of all layers of the tree will not be displayed. Therefore, if you want to have a rounded corner and a shadow, you can only create two overlapping uiviews. One Layer displays the rounded corner and the other layer displays the shadow ......

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.