Ios:quartz2d drawing (Draw simple graphs such as lines, triangles, circles, rectangles, text, etc.)

Source: Internet
Author: User

The previous article has almost detailed the quartz2d of all the knowledge, this and the following is not nonsense, mainly with concrete examples to demonstrate the effect of the drawing.

Here we first draw some simple graphs (such as lines, triangles, circles, rectangles, text, images), which can be drawn in two ways, one through context and the other through a path. The following is a demonstration of drawing triangles in two ways.

to draw a basic graphic, you need to override it in the operation's view class -(void) DrawRect: (CGRect) Rect method and draws a graphic in the method. Drawing an image can either override the method drawing or override the method, and it has a well-encapsulated method. Here, I draw a graph by customizing a view class and associating the view of the controller with this custom class. The concrete examples are as follows:

1. Create a custom view class DemoView, associated controller view.

2. In the Custom view class-(void) DrawRect: (CGRect) The Rect method is plotted:

All calling methods are written in-(void) DrawRect: (cgrect) rect

- (void) DrawRect: (cgrect) rect{//1. Get the context of the drawingCgcontextref context =Uigraphicsgetcurrentcontext (); //Draw a line[self drawline:context]; //Draw a triangle[self drawtriangle:context]; //Draw a rectangle[self drawrectangle:context]; //Draw a circle[self drawcircle:context]; //Use of Paths[self drawbypath:context]; //Drawing Text[self drawstring:context]; //Paint Image[self drawImage];}

Draw a line

#pragma mark-Draw straight lines (solid and dashed lines)

-(void) DrawLine: (cgcontextref) context{//2. Add a drawing pathCgcontextmovetopoint (Context, -, +); Cgcontextaddlinetopoint (Context, -, +);//End//3. Setting the properties of a drawing//the color of the strokeCGFloat redcolor[4] = {1.0,0.0,0.0,1.0};        Cgcontextsetstrokecolor (context, redcolor); //the color of the fillCGFloat greencolor[4] = {0.0,1.0,0.0,1.0};        Cgcontextsetfillcolor (context, greencolor); //line widthCgcontextsetlinewidth (Context,5); //type of line (dashed)//cgfloat dash[2] = {1.0,2.0}; //Cgcontextsetlinedash (Context, 0,dash, 2); //4. Drawing (settings are filled with strokes)Cgcontextdrawpath (context, Kcgpathfillstroke);}

The drawn lines and dashed lines are:

Draw triangles

#pragma mark-Draw triangles by context (first way)

-(void) Drawtriangle: (cgcontextref) context{//2. Add a drawing pathCgcontextmovetopoint (Context, -, -);//starting pointCgcontextaddlinetopoint (Context, Max, -);//EndCgcontextaddlinetopoint (Context, the, Max);//EndCgcontextaddlinetopoint (Context, -, -);//End//3. Setting the properties of a drawing//the color of the strokeCGFloat redcolor[4] = {1.0,0.0,0.0,1.0};        Cgcontextsetstrokecolor (context, redcolor); //the color of the fillCGFloat greencolor[4] = {0.0,1.0,0.0,1.0};        Cgcontextsetfillcolor (context, greencolor); //line widthCgcontextsetlinewidth (Context,5); //The type of connection point for the line (miter, round fillet, bevel boxer)cgcontextsetlinejoin (context, kcglinejoinround); //4. Drawing (settings are filled with strokes)Cgcontextdrawpath (context, kcgpathfillstroke); }

The triangles that are drawn are:

Draw a rectangle

#pragma mark-Draw a rectangle

-(void) DrawRectangle: (cgcontextref) context{//Add a rectangleCgcontextaddrect (Context, CGRectMake ( -, -, -, -)); //set the properties of a drawing//the color of the strokeCGFloat redcolor[4] = {1.0,0.0,0.0,1.0};        Cgcontextsetstrokecolor (context, redcolor); //the color of the fillCGFloat greencolor[4] = {0.0,1.0,0.0,1.0};        Cgcontextsetfillcolor (context, greencolor); //4. DrawingCgcontextdrawpath (context, Kcgpathfillstroke);}

The rectangle that is drawn is:

Draw a Circle

#pragma mark-Draw circle (circle, Ellipse)

-(void) Drawcircle: (cgcontextref) context{//roundCgcontextaddellipseinrect (Context, CGRectMake ( $, -, -, -)); //EllipseCgcontextaddellipseinrect (Context, CGRectMake ( -, -, -, Max)); //set the properties of a drawing//the color of the strokeCGFloat redcolor[4] = {1.0,0.0,0.0,1.0};        Cgcontextsetstrokecolor (context, redcolor); //the color of the fillCGFloat greencolor[4] = {0.0,1.0,0.0,1.0};        Cgcontextsetfillcolor (context, greencolor); //4. DrawingCgcontextdrawpath (context, Kcgpathfillstroke);}

The positive circles and ellipses are drawn:

Draw a triangle through a path (the second way of drawing)

#pragma mark-Draw triangles by Path

-(void) Drawbypath: (cgcontextref) context{//Create PathCgmutablepathref Path =cgpathcreatemutable (); //add an image to the pathCgpathmovetopoint (Path, NULL, -, -); Cgpathaddlinetopoint (Path, NULL, $, -); Cgpathaddlinetopoint (Path, NULL, Max, $); //set the properties of a drawing[[Uicolor Redcolor]setstroke];//Strokes[[Uicolor Greencolor]setfill];//Fill//[[Uicolor Purplecolor]set]; //both stroke and fill//to add a path to the contextCgcontextaddpath (context, path); //Closed PathCgcontextclosepath (context); //DrawingCgcontextdrawpath (context, kcgpathfillstroke); //Clean upcgpathrelease (path);}

The drawn triangles are:

Draw text

#pragma mark-Draw text (no wrapping, line wrapping)

-(void) DrawString: (cgcontextref) context{nsstring*str =@"Hello World"; //start with a dot no line break[Str Drawatpoint:cgpointmake ( -, -) Withattributes:@{nsfontattributename:[uifont Systemfontofsize: -],nsforegroundcolorattributename:[uicolor Bluecolor]}]; //draw in a rectangle, break the line in excess[Str Drawwithrect:cgrectmake ( -, the, -, -) Options:nsstringdrawinguseslinefragmentorigin Attributes:@{nsfontattributename:[uifont systemFontOfSize: -],nsforegroundcolorattributename:[uicolor Bluecolor]} Context:nil];}

Text that does not wrap and wraps is:

Drawing an image

#pragma mark-Draw an image (draw the original, draw the original in the size of the rectangular area, draw with the lace)

-(void) drawimage{UIImage*imagename = [UIImage imagenamed:@"1.png"]; //Original image size[ImageName Drawatpoint:cgpointmake ( -,430)]; //Change image size[ImageName Drawinrect:cgrectmake ( -, -, -, -)]; //pattern Style drawing image[ImageName Drawaspatterninrect:cgrectmake ( -, -, -, -)];}

The three types of images to be drawn are:

Ios:quartz2d drawing (Draw simple graphs such as lines, triangles, circles, rectangles, text, etc.)

Related Article

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.