-(void) DrawRect: (cgrect) Rect {//Get current Context cgcontextref ctx=uigraphicsgetcurrentcontext (); Save the current context state in the stack cgcontextsavegstate (CTX); Zoom, move processing (need to be set before drawing) CGCONTEXTSCALECTM (CTX, 0.5, 0.5); CGCONTEXTTRANSLATECTM (CTX, 100, 100); CGCONTEXTROTATECTM (CTX, m_pi_4); Stroke Cgcontextmovetopoint (CTX, 10, 10); Cgcontextaddlinetopoint (CTX, 100, 100); Cgcontextaddlinetopoint (CTX, 150, 50); The following two methods can be closed-loop//cgcontextaddlinetopoint (CTX, 10, 10); Cgcontextclosepath (CTX); Render paint, solid and hollow Cgcontextstrokepath (CTX); Cgcontextfillpath (CTX); Save the current context state in the stack cgcontextsavegstate (CTX); Draw a square cgcontextaddrect (CTX, CGRectMake (100, 100, 50, 50)); Set the line width (must be before cgcontextstrokepath)//due to previous rendering of paint, so this property setting does not affect the above triangle, the following color setting is the same//So, false assumption to set two or more properties of the graph, respectively, render the paint once the CG Contextsetlinewidth (CTX, 10); Set the color (same as. The code for the property setting is preceded by the drawing code) Cgcontextsetrgbstrokecolor (CTX, 1, 0, 0, 1); Cgcontextstrokepath (CTX); Set Style CgcontexTmovetopoint (CTX, 20, 160); Cgcontextaddlinetopoint (CTX, 200, 280); Cgcontextaddlinetopoint (CTX, 250, 200); Cgcontextsetlinewidth (CTX, 20); Set the Kinsoku style Cgcontextsetlinecap (CTX, Kcglinecapround); Set the corner style Cgcontextsetlinejoin (CTX, Kcglinejoinround); Cgcontextstrokepath (CTX); Remove the context state stored in the stack and restore it.
The above code setting style does not affect other cgcontextrestoregstate (CTX); Draw Oval Cgcontextaddellipseinrect (CTX, CGRectMake (200, 130, 60, 30)); The following equivalent//cgcontextstrokepath (CTX); Cgcontextdrawpath (CTX, Kcgpathstroke); Draw round Cgcontextaddellipseinrect (CTX, CGRectMake (140, 170, 50, 50)); Cgcontextsetlinewidth (CTX, 3); Cgcontextstrokepath (CTX); Draw Arc Cgcontextaddarc (CTX, M_pi_4, M_PI, 1); Cgcontextstrokepath (CTX); Draw 1/4 circles, as well as set new methods of color Cgcontextmovetopoint (CTX, 10, 230); Cgcontextaddlinetopoint (CTX, 10, 280); Cgcontextaddlinetopoint (CTX, 60, 280); Cgcontextaddarc (CTX, ten, 280, 0,-m_pi_2, 1); [[Uicolor Greencolor] setstroke]; Cgcontextstrokepath (CTX); Draw pictures and text (without having to manually get the context) NSString *[email protected] "an empty work for the Year of Sin"; [str1 Drawatpoint:cgpointzero Withattributes:nil]; UIImage *img=[uiimage imagenamed:@ "001"]; [img Drawatpoint:cgpointmake (10, 10)]; Overlap The picture in a box and sign CGRect rect1=cgrectmake (50, 50, 100, 100); [IMG Drawaspatterninrect:rect1]; Nsmutabledictionary *attr=[[nsmutabledictionary Alloc]init]; Attr[nsforegroundcolorattributename]=[uicolor Whitecolor]; Attr[nsfontattributename]=[uifont Systemfontofsize:13]; [Str1 Drawinrect:cgrectmake (+, withattributes:attr, +)]; Remove the context state stored in the stack and restore it. The above code setting style does not affect other cgcontextrestoregstate (CTX); Crop round head cgcontextaddellipseinrect (CTX, CGRectMake (150, 150, 100, 100)); Cut out a context area according to the Circle, and the later content is filled in the circular context cgcontextclip (CTX); UIImage *img1=[uiimage imagenamed:@ "Me"]; [Img1 Drawatpoint:cgpointmake (150, 150)];}
"iOS Development-80" quartz2d Drawing Brief Introduction: Line/Circle/ellipse/square and context stack management Cgcontextsavegstate/cgcontextrestoregstate