Overview Quartz 2D is a two-dimensional drawing engine that supports both iOS and Mac systems. Quartz 2D can do the job:
- Drawing: lines \ triangles \ rectangles \ Circles \ Arcs, etc.
- Draw text
- Draw \ Generate picture (image)
- Read \ Generate PDF
- \ crop a picture
- Customizing UI Controls
Code implementation Draw Lines
#import "LineView.h"@implementationLineview-(void) DrawRect: (cgrect) rect{//Drawing Code//1. Get the graphics contextCgcontextref CTX =Uigraphicsgetcurrentcontext (); //2. Stitching graphics (path)//Set Segment WidthCgcontextsetlinewidth (CTX,Ten); //Style (round) to set the end of the segment headCgcontextsetlinecap (CTX, Kcglinecapround); //Style (round) to set the inflection point of a segmentCgcontextsetlinejoin (CTX, Kcglinejoinround); /** 1th Segment **/ //Set ColorCgcontextsetrgbstrokecolor (CTX,1,0,0,1); //set a starting pointCgcontextmovetopoint (CTX,Ten,Ten); //Add a line segment to (+)Cgcontextaddlinetopoint (CTX, -, -); //render display to view aboveCgcontextstrokepath (CTX); /** 2nd Segment **/ //Set ColorCgcontextsetrgbstrokecolor (CTX,0,0,1,1); //set a starting pointCgcontextmovetopoint (CTX, $, the); //Add a line segment to (Max, Max)Cgcontextaddlinetopoint (CTX, Max, +); Cgcontextaddlinetopoint (CTX, -, -); //render display to view aboveCgcontextstrokepath (CTX);}@end
Effect
Draw Quadrilateral
/** * Draw quadrilateral*/voidDraw4rect () {//1. Get ContextCgcontextref CTX =Uigraphicsgetcurrentcontext (); //2. Draw a rectangleCgcontextaddrect (CTX, CGRectMake (Ten,Ten, Max, -)); //set: Both solid and hollow colors//Setstroke: Setting the hollow color//Setfill: Set Solid color[[Uicolor Whitecolor]Set]; //3. Drawing GraphicsCgcontextfillpath (CTX);}
Draw triangles
/** * Draw a triangle*/voidDrawtriangle () {//1. Get ContextCgcontextref CTX =Uigraphicsgetcurrentcontext (); //2. Draw a triangleCgcontextmovetopoint (CTX,0,0); Cgcontextaddlinetopoint (CTX, -, -); Cgcontextaddlinetopoint (CTX, Max, the); //Close Path (connection start and last point)Cgcontextclosepath (CTX); //ColorCgcontextsetrgbstrokecolor (CTX,0,1,0,1); //3. Drawing GraphicsCgcontextstrokepath (CTX);}
Draw round and round arcs
/** * Draw arcs*/voidDrawArc () {//1. Get ContextCgcontextref CTX =Uigraphicsgetcurrentcontext (); //2. Draw Arcs//x\y: Center//Radius : Radius//startangle: Starting angle//endangle: End Angle//clockwise: Extension direction of Arc (0: Clockwise, 1: counterclockwise)Cgcontextaddarc (CTX, -, -, -, M_pi_2, M_PI,0); //3. Show what's being paintedCgcontextfillpath (CTX);}/** * Draw a circle*/voiddrawcircle () {//1. Get ContextCgcontextref CTX =Uigraphicsgetcurrentcontext (); //2. Draw a circleCgcontextaddellipseinrect (CTX, CGRectMake ( -,Ten, -, -)); Cgcontextsetlinewidth (CTX,Ten); //3. Show what's being paintedCgcontextstrokepath (CTX);}
Common method splicing function
//create a new starting pointvoidCgcontextmovetopoint (cgcontextref C, CGFloat x, cgfloat y)//Add a new segment to a pointvoidCgcontextaddlinetopoint (cgcontextref C, CGFloat x, cgfloat y)//Add a rectanglevoidCgcontextaddrect (cgcontextref C, cgrect rect)//Add an ellipsevoidCgcontextaddellipseinrect (cgcontextref context, CGRect rect)//Add an arcvoidCgcontextaddarc (cgcontextref C, CGFloat x, cgfloat y, cgfloat radius, cgfloat startangle, CGFloat endangle,intclockwise)//Mode parameter determines the pattern to be drawnvoidCgcontextdrawpath (cgcontextref C, cgpathdrawingmode mode)//draw a hollow pathvoidCgcontextstrokepath (cgcontextref c)//draw a solid pathvoidCgcontextfillpath (Cgcontextref c)
Tip: Functions that are usually started with Cgcontextdraw, Cgcontextstroke, and Cgcontextfill are used to draw the path
Operation of the graphics context stack
// Save the current context copy to the top of the stack (the stack is called the "graphics Context stack") void cgcontextsavegstate (cgcontextref c) // Stack the context of the top of the stack, replacing the current context void Cgcontextrestoregstate (Cgcontextref c)
IOS UI Advanced -1.0 quartz2d