First, draw a straight line
Code:
1//2//YYLINEVIEW.M 3//03-Draw Line 4//5//Created by Apple on 14-6-9. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYlineview.h" @implementation YYlineview12 13 14//When the custom view is first displayed, the DrawRect method is called (void) DRA Wrect: (CGRect) rect16 {17 18//1. Gets the graphics context associated with the current view (because the graphics context determines the output target to draw)/19//If it is called in the DrawRect method Uigraphicsgetcurrentcon The text method gets out of the context of Layer cgcontextref ctx=uigraphicsgetcurrentcontext ();//Do not need *, with ID21 22//2. Drawing (drawing lines), saving drawing information 23//Set starting point Cgcontextmovetopoint (CTX, 20, 100); 25//Set end point Cgcontextaddlinetopoint (CTX, 300, 100); 27 28 29//Set the state of the drawing 30//Set the color of the line to Blue Cgcontextsetrgbstrokecolor (CTX, 0, 1.0, 0, 1.0); 32//Set the width of the line CG Contextsetlinewidth (CTX, 15); 34//Set the line start and end style to rounded corners Cgcontextsetlinecap (CTX, kcglinecapround); 36//Set the style of the line's corners as Rounded Cgcontextsetlinejoin (CTX, Kcglinejoinround); 38//3. Render (Draw a hollow line) Cgcontextstrokepath (CTX); 40 41// Note that lines cannot be rendered asSolid Cgcontextfillpath (CTX); 43 44 45 46//Set Second line 47//Set the beginning of the second line Cgcontextmovetopoint (CTX, 50, 200); 49//Set the end of the second antenna (automatically the end of the previous line as the starting point) cgcontextaddlinetopoint (CTX, 300, 60); 51 52//Set the status of the drawing//C Gcontextsetrgbstrokecolor (CTX, 1.0, 0.7, 0.3, 1.0); 54//The second way to set the color is [[Uicolor Graycolor] set];56//Setting the width of the line 57 Cgcontextsetlinewidth (CTX, 10); 58//Set the start and end style of the line Cgcontextsetlinecap (CTX, Kcglinecapbutt); 60 61//Shading Dye the second line of graphics to view 62//Draw a hollow line of Cgcontextstrokepath (CTX);}65 @end
Effect:
Two, draw a triangle
Code:
1//2// YYRECTVIEW.M 3// 02-Draw Triangle 4//5// Created by Hole Doctor on 14-6-10.6// Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYrectview.h" @implementation YYrectview12-(void) DrawRect: (CGRect) rect15 { //1. Get Graphical context cgcontextref ctx=uigraphicsgetcurrentcontext (); //2. Draw triangle //Set starting point Cgcontextmovetopoint (CTX, +);/ /Set the second point at cgcontextaddlinetopoint (CTX, +, +); Set the third point cgcontextaddlinetopoint (CTX, n, x); +/ /Set end point// Cgcontextaddlinetopoint (CTX, 20, 100) ;/ /close start and end of Cgcontextclosepath (CTX); 3. Render the drawing to layer on the Cgcontextstrokepath ( CTX): }35-Notoginseng @end
Effect:
Tip: Close the start and end Cgcontextclosepath (CTX);
Three, draw quadrilateral
Code:
1//2//YYRECT.M 3//03-Draw Quadrilateral 4//5//Created by Hole Doctor on 14-6-10.6//Copyright (c) 2014 itcast. All rights reserved. 7//8 9 #import "YYrect.h" @implementation YYrect12-(void) DrawRect: (CGRect) rect15 {16 17//1. Get the Graphics Context 18 Cgcontextref Ctx=uigraphicsgetcurrentcontext (); 19//2. Draw Quadrilateral Cgcontextaddrect (CTX, CGRectMake (20, 20, 150, 100 )); 21 22//If you want to set the state of the drawing must be before rendering at//Cgcontextsetrgbstrokecolor (CTX, 1.0, 0, 0, 1.0); 24//Draw what type of graphic (hollow or Solid). What type of method will be used to set the status of//Cgcontextsetrgbfillcolor (CTX, 1.0, 0, 0, 1.0); 26 27//Call OC method to set the color of the drawing 28// [[Uicolor Purplecolor] setfill];29//[[Uicolor Bluecolor] setstroke];30//Call OC Method set drawing color (both solid and hollow) 31// [[Uicolor Greencolor] set];32 [[Uicolor colorwithred:1.0 green:0 blue:0 alpha:1.0] set];33 34 35//3. Rendering Graphics to layer 36//Hollow PNS Cgcontextstrokepath (CTX), 38//Solid Cgcontextfillpath (CTX),}42 @end /pre>
Tip: If you want to set the state of the drawing, it must precede rendering.
Effects (solid and hollow):
Four, draw a circle
Code Listing 1:
-(void) DrawRect: (cgrect) rect{ //1. Get context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); Draw round Cgcontextaddarc (CTX, 0, 2 * m_pi, 0); 3. Rendering (note that the line can only be drawn through the hollow)// Cgcontextfillpath (CTX); Cgcontextstrokepath (CTX); }
Effect:
Code Listing 2:
Draw a circle //1. Get context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. Draw round Cgcontextaddellipseinrect (CTX, CGRectMake (): [[Uicolor Greencolor] set]; 3. Render // Cgcontextstrokepath (CTX); Cgcontextfillpath (CTX);
Effect:
Code Listing 3:
Draw Ellipse //1. Get context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. Draw round Cgcontextaddellipseinrect (CTX, CGRectMake (+, +, +)); [[Uicolor Purplecolor] set]; 3. Render // Cgcontextstrokepath (CTX); Cgcontextfillpath (CTX);
Effect:
Five, Draw arc
Code Listing 1:
Draw Arc //1. Get context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. Draw Arc //x/y Center //RADIUS radius //startangle start Radian //Endangle End of Arc ///Clockwise Draw arc direction (0 clockwise, 1 inverse) Needle) /// Cgcontextaddarc (CTX, 0,-m_pi_2, m_pi_2, +/-); Cgcontextaddarc (CTX, m_pi_2, M_PI, 0); Cgcontextclosepath (CTX); 3. Render // Cgcontextstrokepath (CTX); Cgcontextfillpath (CTX);
Effect:
Code Listing 2:
1. Get context Cgcontextref CTX = Uigraphicsgetcurrentcontext (); 2. appeased map //Draw line Cgcontextmovetopoint (CTX,, +); Cgcontextaddlinetopoint (CTX, +,); Draw Arc Cgcontextaddarc (CTX, m_pi_2, M_PI, 0); Cgcontextaddarc (CTX,-m_pi, m_pi_2, 1); Close path cgcontextclosepath (CTX); [[Uicolor Browncolor]set]; 3. Rendering (note that the line can only be painted by hollow) Cgcontextfillpath (CTX); Cgcontextstrokepath (CTX);
Effect:
iOS Development UI Chapter-quartz2d simple Use (i)