Difference between uiview and calayer

Source: Internet
Author: User

We have been studying core animation for some time. There is no good introduction to core animation on the Internet. There is a special summative introduction on the Apple website, but it seems that there are not many things of the principle. People who see the things are confused and feel that they write the things, in fact, it is assumed that the reader understands the principles of the interface animation technology. Today, we have something to do with Linux. I forgot the SSH password and couldn't reset ssh. I couldn't remember how to set up SSH remote login, I couldn't find it on the Internet again. It was a waste of time and it was terrible to forget to take notes. Given that the core animation content is really complicated and the obj-C language itself has many features, write a memorandum to record it. If an error is found after reading it, don't hesitate to advise.

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
1
-(Class) layerclass {
2
Return ([caeagllayer class]);
3
}
Use GL to draw a subclass of A uiview.

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
1
Graycover = [[calayer alloc] init];
2
Graycover. backgroundcolor = [[uicolor blackcolor] colorwithalphacomponent: 0.2] cgcolor];
3
[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 (This section is a bit inaccurate ).
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 relationship between position and anchorpoint is still dizzy)
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 );

7. Analysis of real examples


This is The iBook flip on the iPhone. If every page is a uiview, I think there are two layers on one page, and the text layer displays the front content, the back layer uses the snapshot of the text layer to flip the affine and paste it behind the text layer. Because you can set a shadow for a layer, you may not use a single layer for the shadow effect. As for the surface effect, I checked a lot of information and did not have any results. Is it estimated that the GL curved surface plot was used?

8. The last one is disgusting.
You can set the rounded corner display for a layer, such as the uibutton effect or shadow display. However, if a layer in the layer tree sets the rounded corner, the shadow effects of all layers in the tree cannot be displayed. 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 .....

 

 

Calayer is part of core animation, which is important but not easy to understand. The following is an excerpt from an article in the garden:

1. uiview is the basis of interface elements in IOS SystemsAll interface elements are inherited from it. It is fully implemented by coreanimation.The real drawing part is managed by a calayer class.. 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 an important attribute layer, which can return its primary calayer instance.

// To access the layer, read the layer attribute calayer * layer = myview. layer of the uiview instance.

All objects inherited from uiview inherit this attribute. This means that you can convert, scale, rotate, or even add animations to other view classes such as navigation bars, tables, text boxes. Each uiview has a layer that controls how the content is finally displayed on the screen.
The layerclass method of uiview can return the class used by the main layer. The subclass of uiview can overload this method to make the uiview display with different calayer values. Sample Code:

- (class)layerClass {   return ([CAEAGLLayer class]);}

The code above allows a subclass of A uiview to be drawn using GL.

3. The calayer of the uiview is similar to the sub-view tree structure of the uiview.You can also add a child layer to its layer to complete some special representations. That is, the calayer layer can be nested. Sample Code:

grayCover = [[CALayer alloc] init];grayCover.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2] CGColor];[self.layer addSubLayer:grayCover];

The above Code adds a black transparent film to the target view.

4. The layer tree of the uiview is inside the system, and three copies are maintained.They are logic trees, where code can be manipulated. The animation tree is an intermediate layer where the system changes attributes and performs various rendering operations. The display tree, the content is the content that is currently being displayed on the screen.

5. animation operation: changes the sublayer (non-primary layer) attribute of the uiview. The system automatically generates an animation.The default animation duration is 0.5 seconds.

6. Coordinate System: the coordinate system of calayer has an anchorpoint attribute more than uiview. The value range is 0 ~ 1 is a proportional value.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, only the upper left corner of the layer is moved to the original middle position. This sentence must be added:
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 ......

7. Rendering: When the update layer is changed, the changes cannot be immediately displayed on the screen. When all layers are ready, you can call the setneedsdisplay method to re-paint the display.

[gameLayer setNeedsDisplay];

To repaint some of the screen areas, use the setneedsdisplayinrect: Method to update the area in the cgrect structure:

[gameLayer setNeedsDisplayInRect:CGRectMake(150.0,100.0,50.0,75.0)];

If you use the core graphics framework to perform rendering, you can directly render the core graphics content. Use renderincontext: to do this.

[gameLayer renderInContext:UIGraphicsGetCurrentContext()];

8. Transformation:To add a 3D or affinetransform to a layer, you can set the transform or affinetransform attributes of the layer.

characterView.layer.transform = CATransform3DMakeScale(-1.0,-1.0,1.0);CGAffineTransform transform = CGAffineTransformMakeRotation(45.0);backgroundView.layer.affineTransform = transform;

9. Deformation:The rendering capability of quartz core allows two-dimensional images to be freely manipulated, just like three-dimensional images. Images can be rotated, scaled, and skewed at any angle in a three-dimensional coordinate system. A set of catransform3d methods provide some magic transformation effects.

Post: http://www.cnblogs.com/pengyingh/articles/2381673.html

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.