#import "MyLayer.h"@implementationMylayer- (void) Drawincontext: (cgcontextref) ctx{//Set Fill ColorCgcontextsetrgbfillcolor (CTX,1,0,0,1); //EllipseCgcontextaddellipseinrect (CTX, CGRectMake (0,0, -, -)); //Fill PathCgcontextfillpath (CTX);}@end
#import "MyView.h"@implementationMyView//Only override Drawrect:if perform custom drawing.//An empty implementation adversely affects performance during animation.- (void) DrawRect: (cgrect) rect {//Drawing CodeCgcontextref CTX=Uigraphicsgetcurrentcontext (); //set RGB Fill ColorCgcontextsetrgbfillcolor (CTX,1,0,0,1); //add ellipse inside RectangleCgcontextaddellipseinrect (CTX, CGRectMake (0,0, -, -)); //Fill PathCgcontextfillpath (CTX);}
UIView *view;
View.layer.delegate = = view;
View 's full display process
1. View.layer will prepare a layer Graphics Contex ( the context of the layers type )
2. Call view.layer.delegate (view) Drawlayer:incontext: and pass in the context that you just prepared
3. View Drawlayer:incontext: The method internally calls the view's DrawRect: method
4. View can implement the drawing code in the DrawRect: method , and everything is finally drawn to the view.layer .
5. The system then copies the contents of the View.layer to the screen , thus completing the view display
#import "ViewController.h"#import "MyView.h"#import "MyLayer.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Calayer*layer =[Calayer layer]; //sizeLayer.bounds = CGRectMake (0,0, -, -); //ColorLayer.backgroundcolor =[Uicolor Blackcolor]. Cgcolor; //Anchor PointLayer.anchorpoint =Cgpointzero; //locationLayer.position = Cgpointmake ( -, -); //AgentLayer.Delegate=Self ; [Layer Setneedsdisplay]; [Self.view.layer Addsublayer:layer]; [Self diylayer]; }#pragmaMark-Proxy method for layers//Drawing Agents- (void) Drawlayer: (Calayer *) layer Incontext: (cgcontextref) ctx{cgcontextsetrgbfillcolor (CTX,1,0,0,1); Cgcontextaddrect (CTX, CGRectMake (0,0, -, -)); Cgcontextfillpath (CTX);}- (void) diylayer{Mylayer*layer =[Mylayer layer]; Layer.bounds= CGRectMake (0,0, -, -); Layer.backgroundcolor=[Uicolor Bluecolor]. Cgcolor; Layer.anchorpoint=Cgpointzero; [Layer Setneedsdisplay]; [Self.view.layer Addsublayer:layer];}@end
Drawing garbage images