quartz2d Introduction (subsequent applications will be relevant)
First Part Draw a line
code example:
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref Cxcontext =Uigraphicsgetcurrentcontext (); //Start Drawing//Set Line StartCgcontextmovetopoint (Cxcontext,0, -); //set the midpoint of a lineCgcontextaddlinetopoint (Cxcontext, -, -); //RenderingCgcontextstrokepath (Cxcontext);}
:
We only used four lines of code to draw a line in the view, but it would feel very dull, knowledge of a black straight line.
So we'll give him some attributes.
In order to test, I only added color to him first.
Example code:
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref Cxcontext =Uigraphicsgetcurrentcontext (); //Start Drawing//Set Line StartCgcontextmovetopoint (Cxcontext,0, -); //set the midpoint of a lineCgcontextaddlinetopoint (Cxcontext, -, -); //Set ColorCgcontextsetrgbstrokecolor (Cxcontext,1,0,0,1); //RenderingCgcontextstrokepath (Cxcontext);}
:
Can see him turn red.
To analyze the code I've added, I can guess the width and so on.
I'm adding the width below.
Example code:
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref Cxcontext =Uigraphicsgetcurrentcontext (); //Start Drawing//Set Line StartCgcontextmovetopoint (Cxcontext,0, -); //set the midpoint of a lineCgcontextaddlinetopoint (Cxcontext, -, -); //Set ColorCgcontextsetrgbstrokecolor (Cxcontext,1,0,0,1); //Set WidthCgcontextsetlinewidth (Cxcontext,Ten); //RenderingCgcontextstrokepath (Cxcontext);}
:
It's easy to draw a straight line here, but it's not hard to imagine if we're spending more than one test, if we now spend two lines without intersections (we can continue adding lines through Cgcontextaddlinetopoint).
Here's the path, we can draw lines and differentiate them.
Example code:
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref Cxcontext =Uigraphicsgetcurrentcontext (); //Create 2 PathsCgmutablepathref path1 =cgpathcreatemutable (); Cgmutablepathref path2=cgpathcreatemutable (); //Start Drawing//draw the first lineCgpathmovetopoint (path1, NULL,0, -); Cgpathaddlinetopoint (path1, NULL, -, -); //draw a second lineCgpathmovetopoint (path2, NULL,0, -); Cgpathaddlinetopoint (path2, NULL, -, -); //add a path to the contextCgcontextaddpath (Cxcontext, path1); Cgcontextaddpath (Cxcontext, path2); //RenderingCgcontextstrokepath (Cxcontext); //release because it is CG and need to be released manuallycgpathrelease (path1); Cgpathrelease (path2);}
:
Part IIDraw a graphic
Example code:
-(void) DrawRect: (cgrect) rect{ // Get Graphics context cgcontextref cxcontext = Uigraphicsgetcurrentcontext (); // Draw a rectangle Cgcontextaddrect (Cxcontext, CGRectMake ()); // Rendering Cgcontextstrokepath (cxcontext); }
:
Example code:
-(void) DrawRect: (cgrect) rect{ // Get Graphics context cgcontextref cxcontext = Uigraphicsgetcurrentcontext (); // Draw a circle - - - 0 0 ); // Rendering Cgcontextstrokepath (cxcontext); }
:
Part IIIDraw text
Example code:
- (void) DrawRect: (cgrect) rect{//Get the graphics contextCgcontextref Cxcontext =Uigraphicsgetcurrentcontext (); //Draw a rectangleCgcontextaddrect (Cxcontext, CGRectMake ( -, -, -, -)); //RenderingCgcontextstrokepath (Cxcontext); //text contentNSString *str =@"Xu Bao love to eat fish Xu Bao love to eat fish Xu Bao love to eat fish Xu Bao love to eat fish Xu Bao love to eat fish"; //draws text to the specified area after wrapping the range to extract the extent without displaying[Str Drawinrect:cgrectmake ( -, -, -, -) Withattributes:nil]; //draw text to a specified point//[str drawatpoint:cgpointmake (0, 0) Withattributes:nil]; }
:
Fourth PartDrawing pictures
Instance code:
-(void) DrawRect: (cgrect) rect{ * image = [UIImage imagenamed:@ "2.jpg "]; // tiled [Image drawAsPatternInRect:self.bounds]; }
:
Example code:
-(void) DrawRect: (cgrect) rect{ * image = [UIImage imagenamed:@ "2.jpg" ]; // Stretch [Image drawInRect:self.bounds]; }
:
Instance code:
-(void) DrawRect: (cgrect) rect{ * image = [UIImage imagenamed:@ "2.jpg" ]; // the position of the original image (upper left point of the picture) [Image DrawAtPoint:self.center]; }
:
About IOS quartz2d