First, the complete process of drawing the program starts, displays the custom view. When the program first appears in front of us, the program calls the DrawRect: method, gets the graphics context (in memory), and then uses the graphical context to save the drawing information, which can be understood as an area in the graphical context to hold the drawing information, An area is used to save the state of the drawing (line width, fillet, color). The straight line is not drawn directly to the view, it can be understood that there is a separate area in the graphics context to draw the graph first, when the rendering method is called, and then the drawing of the graph displayed to the view up. In the drawing area, you will find the corresponding status information (line width, fillet, color) in the plot status area, and then draw the first line in the drawing area. In fact, before rendering, the line has been drawn in the drawing area. Description: These and the program code block in this article do not have a one by one correspondence, just to illustrate the complete process of drawing. When the rendering method is called, the drawing graphics area is displayed directly on the view, which is what we see. When you draw the second bar, if you do not reset the drawing state, you can find that the drawing state you used to draw the first antenna is also saved in the graphics context, and the second line is set according to the first line (previous drawing state) before the second line is rendered. After rendering, the second line is displayed on the screen. Reference code:
1- (void) DrawRect: (cgrect) Rect2 {3 //Get Context4Cgcontextref ctx=Uigraphicsgetcurrentcontext ();5 //Drawing6 //First line7Cgcontextmovetopoint (CTX, -, -);8Cgcontextaddlinetopoint (CTX, -, the);9 Ten //set the state of the first line One //set the width of a line ACgcontextsetlinewidth (CTX, A); - //set the color of a line -[[Uicolor Browncolor]Set]; the //set the style at both ends of the line to rounded corners - Cgcontextsetlinecap (ctx,kcglinecapround); - //to render a line - Cgcontextstrokepath (CTX); + - //Second Line +Cgcontextmovetopoint (CTX, +, $); ACgcontextaddlinetopoint (CTX, the, -); at //Rendering - Cgcontextstrokepath (CTX); -}View Code
If the state is cleared, the second line is drawn in the drawing area before rendering, and the current drawing information is searched (changed-emptied), the second line is drawn according to the drawing information, and the second line is displayed on the view when the rendering method is called. Reference code:
1- (void) DrawRect: (cgrect) Rect2 {3 //Get Context4Cgcontextref ctx=Uigraphicsgetcurrentcontext ();5 //Drawing6 //First line7Cgcontextmovetopoint (CTX, -, -);8Cgcontextaddlinetopoint (CTX, -, the);9 Ten //set the state of the first line One //set the width of a line ACgcontextsetlinewidth (CTX, A); - //set the color of a line -[[Uicolor Browncolor]Set]; the //set the style at both ends of the line to rounded corners - Cgcontextsetlinecap (ctx,kcglinecapround); - //to render a line - Cgcontextstrokepath (CTX); + - //Second Line +Cgcontextmovetopoint (CTX, +, $); ACgcontextaddlinetopoint (CTX, the, -); at - //Empty State -Cgcontextsetlinewidth (CTX,1); -[[Uicolor Blackcolor]Set]; - Cgcontextsetlinecap (Ctx,kcglinecapbutt); - in //Rendering - Cgcontextstrokepath (CTX); to}View Code
Third, the graphics context stack1. Simply specify that after you get the graphics context,
Cgcontextsavegstate (CTX);
method to copy the currently acquired context and save a copy of the most pure graphical context. Before drawing the second line, use the Cgcontextrestoregstate (CTX) method to restore the most pure graphical context saved at the beginning. Code:
1- (void) DrawRect: (cgrect) Rect2 {3 //1. Get Context4Cgcontextref CTX =Uigraphicsgetcurrentcontext ();5 6 //put the CTX copy on the stack7 cgcontextsavegstate (CTX);8 9 //Set Drawing statusTenCgcontextsetlinewidth (CTX,Ten); One[[Uicolor Redcolor]Set]; A Cgcontextsetlinecap (CTX, kcglinecapround); - - //1th Line theCgcontextmovetopoint (CTX, -, -); -Cgcontextaddlinetopoint (CTX, -, the); - - Cgcontextstrokepath (CTX); + - //stacks the context of the top of the stack, replacing the current context + cgcontextrestoregstate (CTX); A at - //2nd Line -Cgcontextmovetopoint (CTX,Ten, -); -Cgcontextaddlinetopoint (CTX, -,290); - - Cgcontextstrokepath (CTX); in //Cgcontextdrawpath (CTX, kcgpathstroke); -}View Code
Operating effect:
2. Graphical context stack mechanism when you draw the first line, a copy of the current graphics context is saved to the graphics context stack. When the second line is drawn, the drawing information of the top of the stack is taken out of the graph context stack, and as the state information of the second line, the state information of the second line is drawn accordingly (the first saved graphical context).Note: Stored in the stack a few times, then you can take a few times (such as can not be saved 1 times, take two times, in the second time, the stack is empty will directly hang off).
"IOS" quartz2d Graphics context