Drawing garbage images

Source: Internet
Author: User


#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.