Directory of this document
- I. Method of the custom layer 1
- Ii. method of the custom layer 2
- Third, other
Custom layer, is actually drawing on the layer, a total of 2 methods, the following detailed introduction.
Back to top one, custom layer Method 1
Method Description: Create a Calayer subclass, and then overwrite the Drawincontext: method, use the Quartz2d API to draw
1. Create a Calayer subclass
2. Overwrite drawincontext in. m file: method, Draw inside
1 @implementation mjlayer 2 3 #pragma mark draws a solid triangle 4-(void) Drawincontext: (cgcontextref) CTX {5 //set to Blue 6 Cgcontextsetrgbfillcolor (CTX, 0, 0, 1, 1); 7 8 9 //Set starting point cgcontextmovetopoint (CTX, 0);// from (50, 0) to (0, +) Cgcontextaddlinetopoint (CTX, 0, +); //from (0, 100) to Cgcontextaddlinetopoint (CTX, 100, 100); // merge path, connect start and end of Cgcontextclosepath (CTX); + //Draw path Cgcontextfillpath (CTX); 20} @end
3. Add layers to the screen in the controller
1 Mjlayer *layer = [Mjlayer layer];2//Set the width of the layer 3 Layer.bounds = CGRectMake (0, 0, 100, 100); 4//Set the position of the layer 5 layer.position = CG Pointmake (100, 100); 6//Start drawing layer 7 [Layer setneedsdisplay];8 [Self.view.layer addsublayer:layer];
Note that line 7th requires that the Setneedsdisplay method be called before the Drawincontext: Call to the method is triggered, and then the drawing
Back to top two, custom layer Method 2
Method Description: Set Calayer delegate, and then let delegate implement Drawlayer:incontext: Method, Calayer delegate is called when Drawlayer:incontext requires drawing: method to draw.
* Note here: You can no longer set a uiview to Calayer delegate, because the UIView object is already the delegate of its internal root layer, and the delegate that is set to another layer will be problematic. The default diagram for UIView and its internal calayer:
1. Create a new layer, set the delegate, and add it to the layer of the Controller's view
1 Calayer *layer = [Calayer layer]; 2//Set delegate 3 layer.delegate = self; 4//Set the width height of the layer 5 Layer.bounds = cgrectmake (0, 0, 100, 100); 6//Set the position of the layer 7 layer.position = cgpointmake (100, 100); 8//Start drawing layer 9 [Layer setneedsdisplay];10 [Self.view.layer addsublayer:layer];
* Calayer delegate is set on line 3rd, where self refers to the controller
* Note the 9th line, need to call Setneedsdisplay this method, will notify delegate to draw
2. Let the Calayer delegate (the controller previously set) Implement Drawlayer:incontext: Method
1 #pragma mark draws a rectangular box 2-(void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) CTX {3 //Set Blue 4 CGCONTEXTSETR Gbstrokecolor (CTX, 0, 0, 1, 1); 5 //Set Border Width 6 cgcontextsetlinewidth (CTX, ten); 7 8 //Add a rectangle as large as the layer to the path 9 cgcontextaddrect (CTX, Layer.bounds) //Draw Path 12
Back to top three, other 1. Summary
Whichever method you take to customize the layer, you must call Calayer's Setneedsdisplay method to draw properly.
Detailed display procedure for 2.UIView
* When UIView needs to be displayed, its inner layer prepares a cgcontextref (graphical context) and then calls the Drawlayer:incontext of delegate (here is UIView): method, And pass in the Cgcontextref object that is already ready. and UIView in Drawlayer:incontext: The method calls its own DrawRect: method
* Usually in DrawRect: through Uigraphicsgetcurrentcontext () is the Cgcontextref object that is passed in by the layer, all the drawings completed in DrawRect: are filled into the cgcontextref of the layer, Then be copied to the screen
OC Language Knowledge 10