iOS Development UI Chapter-calayer (custom layer)
One, the first way
1. Brief description
Before you want to draw something in view, you need to customize the view, create a class associated with it, let the class inherit from UIView, and then rewrite its drawrect: method, and then draw in the method.
To draw a graphic:(1) Get context(2) Drawing graphics(3) rendering graphicsIf you draw something on a layer, it is similar to the above procedure. code example: Create a new class to inherit from the Calayeryymylayer.m file
1//2//Yymylayer.m3//05-Custom Layer (1)4//5//Created by Apple on 14-6-21.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYMylayer.h"1011@implementationYymylayer12//Override this method to draw a graphic within the method13-(void) Drawincontext: (cgcontextref) CTX14{15//1. Drawing graphics16//Draw a CircleCgcontextaddellipseinrect (CTX, CGRectMake (40M40M100,10018 // set properties (color) 19 // [[Uicolor yellowcolor]set];20 Cgcontextsetrgbfillcolor (CTX, 0, 0, 1, 122 //2. Render 23 Cgcontextfillpath (CTX); }25 @end In the controller, create a custom class
1//2//Yyviewcontroller.m3//05-Custom Layer (1)4//5//Created by Apple on 14-6-21.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"10#import"YYMylayer.h"1112@interfaceYyviewcontroller ()1314@end1516@implementationYyviewcontroller1718-(void) Viewdidload19{20[Super Viewdidload];21st22//1. Create a custom layerYymylayer *layer=[Yymylayer layer];24//2. Setting the properties of a layerLayer.backgroundcolor=[Uicolor Browncolor]. Cgcolor;Layer.bounds=cgrectmake (0,0,200,150);Layer.anchorpoint=Cgpointzero;Layer.position=cgpointmake (100,100);layer.cornerradius=20;Layer.shadowcolor=[Uicolor Blackcolor]. Cgcolor;Layer.shadowoffset=cgsizemake (ten ); layer.shadowopacity=0.6; [Layer setneedsdisplay]; + //3. Add Layer [Self.view.layer Addsublayer:layer]; Notoginseng @end Note the point:(1) The default is colorless and will not be displayed. To make the drawing appear, you also need to set the color of the graphic. Note You cannot use the class in the UI framework directly (2) in a custom layer-(void) Drawincontext: The method does not call itself, only by itself through the Setneeddisplay method call, in the view to draw things drawrect: method is called automatically when the view is first displayed. Achieve the effect: 2. Expand the drawing instructions in UIView
1#import"YYVIEW.h"23@implementationYyview456-(void) DrawRect: (cgrect) rect7{8//1. Get context9 Cgcontextref ctx=Uigraphicsgetcurrentcontext ();10//2. Drawing graphicsCgcontextaddellipseinrect (CTX, CGRectMake (40M40M100,100));12//Set properties (color)13 // [Uicolor Yellowcolor]set]; 14 Cgcontextsetrgbfillcolor (CTX, 0, 0, 1, 115 16 //3. Render 17 Cgcontextfillpath (CTX); //19 [Self.layer drawincontext:ctx];
Description: Draws a graphic in UIView, and gets the context of the view corresponding to the layer. When rendering, the graphic is rendered to the corresponding layer.
When performing rendering operations, it is inherently equivalent to executing [self.layer drawincontext:ctx];
Ii. Second Way
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.
code example:
1//2//Yyviewcontroller.m3//06-Custom Layer (2)4//5//Created by Apple on 14-6-21.6//Copyright (c) 2014 itcase. All rights reserved.78#import"YYViewController.h"9@interfaceYyviewcontroller ()10@end1112@implementationYyviewcontroller1314-(void) Viewdidload15{16[Super Viewdidload];17//1. Create a custom layerCalayer *layer=[Calayer layer];19//2. Setting the properties of a layerLayer.backgroundcolor=[Uicolor Browncolor]. Cgcolor;Layer.bounds=cgrectmake (0,0,200,150);Layer.anchorpoint=Cgpointzero;Layer.position=cgpointmake (100,100);layer.cornerradius=20;Layer.shadowcolor=[Uicolor Blackcolor]. Cgcolor;Layer.shadowoffset=cgsizemake (10,20);layer.shadowopacity=0.6;2829//Set up ProxyLayer.Delegate=Self31[Layer Setneedsdisplay];32//3. Add Layer33[Self.view.layer Addsublayer:layer];34}3536-(void) Drawlayer: (Calayer *) Layer Incontext: (cgcontextref) CTX37{38//1. Drawing graphics39//Draw a CircleCgcontextaddellipseinrect (CTX, CGRectMake (40M40M100,10041 // set properties (color) 42 // [[Uicolor yellowcolor]set];43 Cgcontextsetrgbfillcolor (CTX, 0, 0, 1, 145 //2. Render 46 Cgcontextfillpath (CTX); }48 @end
Implementation results:
Note: 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.
When setting up an agent, it does not require us to comply with the agreement, stating that this method is NSObject, there is no need for additional display compliance protocol.tip: If you want to set a proxy for a class in the future, but this agent does not require us to comply with any specific protocol, then we can think of this protocol method is nsobject inside.
Iii. Additional Information(1) Regardless of which method you take to customize the layer, you must call Calayer's Setneedsdisplay method to draw properly. (2) Detailed realistic process: when UIView needs to be displayed, its inner layer prepares a cgcontextref (graphics context) and then calls delegate (here is UIView) Drawlayer:incontext: Method, And pass in the Cgcontextref object that is already ready. and UIView in the Drawlayer:incontext: method will also call their 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, It is then copied to the screen.
iOS Development UI Chapter-calayer (custom layer)