The layer is what we call the Calayer layer, and the view is what we call the UIView layer.
UIView has three more important layout properties: Frame,bounds and Center,calayer are referred to as frame,bounds and position. To be clear, the layer uses "position" and the View uses "center", but they all represent the same value.
For a view or a layer, frame is not a very clear attribute, it is actually a virtual property, calculated based on bounds,position and transform, so when any of these values change, the frame will change. Conversely, changing the value of a frame also affects the values in them.
There is a long-standing misconception: frame width and height of the bounds are always consistent.
In fact, when a layer is transformed, such as rotation or scaling, the frame actually represents the entire axis-aligned rectangular area after the layer is rotated, which means that frame's width and height may no longer be the same as the bounds width.
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
Anchor Point:
Both the Center property of the view and the position property of the layer specify the position of the anchorpoint relative to the parent layer. The anchorpoint of a layer controls the position of its frame by position, and you can think of Anchorpoint as a handle to move the layer. By default, Anchorpoint is positioned at the midpoint of the layer, so the layer will be centered at this point. The Anchorpoint property is not exposed by the UIView interface, which is why the position property of the view is called "center". But the layer's anchorpoint can be moved, for example, you can put it in the upper-left corner of the layer frame, so the contents of the layer will move in the direction of the lower-right corner of the position (Figure 3.3), not centered.
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
Three-dimensional coordinate system:
Unlike UIView's strict two-dimensional coordinate system, Calayer exists in a three-dimensional space. In addition to the position and Anchorpoint properties we have discussed, Calayer has two other properties, Zposition and Anchorpointz, which are floating-point types that describe the position of the layer on the z-axis.
Practical Application of Zposition:
In addition to moving and rotating layers in three-dimensional space, the most useful feature is to change the order in which layers are displayed.
Self.greenView.layer.zPosition = 1.0f;
You can use this setting to make the view appear before the view that is smaller than its zposition.
In fact, there is no need to add too much, the view is very thin, so give zposition a pixel to make the green view forward, of course, 0.1 or 0.0001 can do it, but it is best not to do so, because the calculation of floating-point type rounding can cause some inconvenience.
However, it is important to note that:
The Zposition property can significantly change the order of the screen layers, but cannot change the order in which the events are passed
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
Handling of touch events:
Calayer does not care about any response chain events, so it cannot handle touch events or gestures directly. But it has a series of ways to help you deal with events:-containspoint: and-hittest:.
Calayer *layer = [Self.layerView.layer hittest:point];
You can use this method to get a layer that is touched on a layer;
》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》
Settings for the shadow of the layer:
Shadowopacity: The default value is 0, and the setting is greater than 0, and the shadow can be displayed under any intent layer. Shadowopacity is a floating-point number that must be between 0.0 (invisible) and 1.0 (completely opaque). If set to 1.0, a slightly blurred black shadow is displayed slightly above the layer. To change the performance of the shadow, you can use another three properties of Calayer: Shadowcolor,shadowoffset and Shadowradius.
Shadowcolor: The color of the shadow, general black used more, is also the cgcolorref type.
Shadowoffset: The property controls the direction and distance of the shadow. It is a cgsize value, the width controls the lateral displacement of the shadow, and the height controls the longitudinal displacement. The default value for Shadowoffset is {0,-3}, meaning that the shadow has an upward displacement of 3 points relative to the y-axis.
Shadowradius: The attribute controls the blur of the shadow, and when its value is 0, the shadow has a very definite boundary line as the view. When the value is getting bigger, the boundary line will look more and more blurred and natural. Apple's own application design is more in favor of the natural shadow, so a non-0 value is more appropriate.
Shadowpath property: The path used to crop the shadow of the layer, Shadowpath is a cgpathref type (a pointer to Cgpath). Cgpath is a core graphics object used to specify any vector graphic. This property allows us to specify the shape of the shadow separately from the layer shape.
Usage:
Cgmutablepathref Circlepath = cgpathcreatemutable ();
Cgpathaddellipseinrect (Circlepath, NULL, self.layerView2.bounds);
Self.layerView2.layer.shadowPath = Circlepath;
Cgpathrelease (Circlepath);
Knowledge summary of layers and view layers