To customize a layer, you only need to build a class to inherit the Calayer method
There are two ways to make a custom layer initialize with a drawing on it.
Overriding the Drawincontext method
Customize the layer below the method, and then you must customize the Layer object to display the call
[Layer Setneedsdisplay] Before this method is called;
customizing layer Files
- (void)drawInContext:(CGContextRef)ctx { CGContextSetRGBFillColor(ctx, 1, 0, 0, 1);//设置红色 CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50));//画圆 CGContextFillPath(ctx);}
Controller files
Testlayer *layer = [Testlayer layer];Layer. Bounds= CGRectMake (0,0, -, -);Layer. Position= Cgpointmake ( -, -);Layer. Anchorpoint= Cgpointmake (0,0);Layer. BackgroundColor= [Uicolor Bluecolor]. Cgcolor;Layer. Cornerradius=Ten;[Layer Setneedsdisplay]or//must be shown to call this method before calling the method above[Self. View. LayerAddsublayer:layer];
Using proxies
First the agent controller does not need to follow the protocol, because this Protocol is a classification, all the NSObject are implemented
Method of implementing agent for controller
#pragma marks -实现代理的方法- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx { CGContextSetRGBFillColor(ctx, 1, 0, 0, 1); CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 50, 50)); CGContextFillPath(ctx);}
Call
Testlayer *layer = [Testlayer layer];Layer. Bounds= CGRectMake (0,0, -, -);Layer. Position= Cgpointmake ( -, -);Layer. Anchorpoint= Cgpointmake (0,0);Layer. BackgroundColor= [Uicolor Bluecolor]. Cgcolor;Layer. Cornerradius=Ten;Layer. Delegate= Self;[Layer Setneedsdisplay];//must display the call, or the proxy method will not be invoked[Self. View. LayerAddsublayer:layer];
Two ways to customize layers for iOS development-layers