Calayer's homestay map (that is, the diagram contained in the layer)
Calayer Property
1.contents:
Layer.contents = (__bridge ID) image. Cgimage;
2.contentGravity:
With UIView Contentmode
The purpose of contentsgravity is to determine how the content is aligned in the boundaries of the layer.
3.Contentsscale
The Contentsscale property is actually part of a screen mechanism that supports high-resolution (aka hi-dpi or retina). It is used to determine the amount of space that should be created for a homestay map when the layer is drawn, and the stretching of the picture that needs to be displayed (assuming that the contentsgravity attribute is not set).
4.Masktobounds
With UIView Clipstobounds
Decide whether to show content beyond the bounds
5.contentsRect
The default contentsrect is {0, 0, 1, 1}, which means that the entire homestay map is visible by default, and if we specify a smaller rectangle, the picture will be cropped (2.6)
Often used to flatten pictures
6.Contentscenter
Similar to UIImage resizableimagewithcapinsets
Controlling Contentscenter properties with the interface Builder probe window
Contentscenter is actually a cgrect that defines a fixed border and an area to stretch on the layer
Custome Drawing
The value assigned to contents Cgimage is not the only way to set up a homestay map. We can also directly draw a homestay map with the core graphics directly. Ability to customize drawing by inheriting UIView and implementing-drawrect: Method.
-drawrect
If you don't need a homestay map, don't create this method, which can lead to wasted CPU resources and memory.
When it needs to be redrawn, Calayer will ask its agent to give him a homestay map to display. It does this by invoking the following method:
1 |
(void)displayLayer:(CALayerCALayer *)layer; |
Take advantage of this opportunity, if the agent wants to set the contents property directly, it can do so, otherwise there is no other way to call. If the agent does not implement the-displaylayer: method, Calayer will instead attempt to invoke the following method:
1 |
- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx; |
Before calling this method, Calayer created an empty homestay map of the appropriate size (size determined by bounds and Contentsscale) and a drawing context for the core graphics, which prepares for drawing the homestay map, which he passed in as a CTX parameter.
Unlike UIView, when a layer is displayed on the screen, Calayer does not automatically redraw its contents. It gives the developer the right to redraw, and calls the-display explicitly.
Although we did not use the Maskstobounds property, the drawn circle is still clipped along the boundary. This is because when you use Calayerdelegate to draw a homestay map, there is no rendering support for content outside the bounds.
When UIView creates its host layer, it automatically sets the layer's delegate to its own
Coordinate systems of UIView and Calayer
The Frame,bounds and Center properties of a view are just access methods, and when the frame that manipulates the view is actually changing the frame that is calayer below the view, it is not possible to change the view frame independently of the layer.
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.
Remember that when you transform a layer, such as rotation or zooming, the frame actually represents a rectangular area that spans the entire axis aligned after the layer rotates, meaning that frame's width and height may no longer be the same as the bounds width.
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".
Calayer provides a number of tool-class methods for layer transformations between different coordinate systems:
1234 |
- (CGPoint)convertPoint:(CGPoint)point fromLayer:(CALayer *)layer; - (CGPoint)convertPoint:(CGPoint)point toLayer:(CALayer *)layer; - (CGRect)convertRect:(CGRect)rect fromLayer:(CALayer *)layer; - (CGRect)convertRect:(CGRect)rect toLayer:(CALayer *)layer; |
These methods can convert a point or rectangle defined in one layer coordinate system to a point or rectangle under another layer coordinate system.
geometryflipped
It determines whether a layer's coordinates are flipped vertically relative to the parent layer
Calayer also has two additional properties, Zposition and Anchorpointz, both of which are floating-point types that describe the position of the layer on the z-axis.
The most useful function of zposition is to change the order in which layers are displayed.
The Zposition property can significantly change the order of the screen layers, but cannot change the order in which the events are passed.
Ios-core-animation-advanced-techniques (a) Summary