In iOS, the things you can see and touch are basically uiview, such as a button, a label, a text input box, which are all uiview;
In fact, UIView can be displayed on the screen entirely because of a layer inside it
When you create a UIView object, a layer (that is, the Calayer object) is automatically created inside UIView, which can be accessed through the Layer property of UIView.
@property (Nonatomic,readonly,retain) Calayer *layer;
Each uiview internally is associated with a calayer, which we can call the layer as Rootlayer (root layer)
The next step is to introduce the two ways of customizing Calayer: Inheritance mode, proxy mode. Before explaining these two ways, first explain the UIView process
Each uiview is a proxy for its own internal layer, and the code is interpreted as: self.layer.delegate = self; Self represents a view
1. When we create a UIView each time, View.layer first prepares a layer type context
2. Call View.layer.delegate (view itself)-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTx method, and pass in the prepared context in the first step
3.-(void) Drawlayer: (Calayer *) layer Incontext: (CGCONTEXTREF) CTX also calls-(void) DrawRect: (cgrect) rect
4.view-(void) DrawRect: (CGRect) The Rect method implements the code that paints the object, and the drawn image is eventually drawn to the View.layer
5. The system will copy the contents of the View.layer to the screen, it will complete the view display
The analysis of the above process, we understand that when we customize the UIView, in-(void) DrawRect: (cgrect) rect when writing drawing code, first through the Uigraphicsgetcurrentcontext () to obtain-( void) Drawlayer: (Calayer *) layer Incontext: (CGCONTEXTREF) The context passed in the CTX method,
Next, we'll explain how to customize Calayer by inheritance
1. First create a class that inherits Calayer
2 in the HFLAYER.M
In this method, you can draw a graphic
Next
It can be found that the layer object created by our custom layer class has a circle in it-(void) Drawincontext: (cgcontextref) The graph drawn in CTX
The second way: Customize the layer by proxy
After setting the proxy object, call-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX
This time, look
Everyone must have doubts about the place, first: Layer.delegate = self; Why not abide by the agreement? There will be-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTx method
There is no agreement on the proxy properties of layer; What we often see is
This proxy property.
Second: Both the UIView and the layer have a setneeddisplay redraw method.
No matter what kind of custom layer, the final remember to redraw, otherwise there will be no effect;
As long as we will use these two kinds of custom layer of the way you can, the principle, I also do not understand, just know a ballpark.
Some students will say that the location of the circle is not in the center of the layer, this is because of the coordinates set the reason, modify the sitting punctuation will be in the middle;
As you can see, layer can also achieve the same effect as Uiimageview. Can show things, so what is the use of layer or Uiimagview?
This depends on the actual function, if only to display the picture, do not need to interact with the user. You can use layer, because the layer is only responsible for displaying the image, less interaction with the user, can improve performance;
If the display of the picture to interact with the user, you can only use UIView to deal with, can not use layer;
Two ways to customize layer for IOS