The Graphics context is either a graphics contextual or a canvas, and we can do the painting on it, and when we do, we'll put the canvas in our view and view it as a frame.
Cgcontextref function is powerful, we can draw various graphics with the help of it. Using these techniques flexibly in the development process can help us to provide the code level.
First, create a custom CustomView class with an integrated UIView.
Implement code in CUSTOMVIEW.M.
Copy Code code as follows:
#import <QuartzCore/QuartzCore.h>
Overwrites the Dranrect method, drawing the graph in this method.
Once CustomView is written, it needs to be used in the view controller.
How to use:
Copy Code code as follows:
CustomView *customview = [[CustomView alloc]initwithframe:cgrectmake (0, 0, Self.view.frame.size.width, Self.view.frame.size.height)];
[Self.view Addsubview:customview];
Write text
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
Get current artboard
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Color
Cgcontextsetrgbstrokecolor (CTX, 0.2, 0.2, 0.2, 1.0);
The width of the drawing line
Cgcontextsetlinewidth (CTX, 0.25);
Start writing
[@ "I am writing" (Drawinrect:cgrectmake) Withfont:font];
[Super Drawrect:rect];
}
This piece of code can be very beautiful to write four characters: I am text. It's easy to understand that every sentence has a comment.
Draw a straight line
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
Get current artboard
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Color
Cgcontextsetrgbstrokecolor (CTX, 0.2, 0.2, 0.2, 1.0);
The width of the drawing line
Cgcontextsetlinewidth (CTX, 0.25);
Top Horizontal
Cgcontextmovetopoint (CTX, 0, 10);
Cgcontextaddlinetopoint (CTX, Self.bounds.size.width, 10);
Cgcontextstrokepath (CTX);
[Super Drawrect:rect];
}
Draw an arc
Copy Code code as follows:
Cgcontextsetrgbstrokecolor (context, 0, 0, 1, 1);//Change brush color
Cgcontextmovetopoint (context, 140, 80);/Start coordinates P1
Cgcontextaddarctopoint (cgcontextref C, cgfloat x1, cgfloat y1,cgfloat x2, CGFloat y2, cgfloat radius)
X1,y1 and P1 form a line of coordinates p2,x2,y2 end coordinates with P3 to form a line P3,radius radius, note that need to calculate the length of the radius,
Cgcontextaddarctopoint (context, 148, 68, 156, 80, 10);
Cgcontextstrokepath (context);//painting path
Draw a Circle
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
Get current artboard
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Color
Cgcontextsetrgbstrokecolor (CTX, 0.2, 0.2, 0.2, 1.0);
The width of the drawing line
Cgcontextsetlinewidth (CTX, 0.25);
void Cgcontextaddarc (Cgcontextref c,cgfloat x, cgfloat y,cgfloat radius,cgfloat startangle,cgfloat endAngle, int Clockwise) 1 radians =180°/π (≈57.3°) degrees = radians x180°/π360°=360xπ/180 =2π radians
X,y is a circle point coordinate, radius radius, startangle is the starting Radian, Endangle is the ending Radian, clockwise 0 is clockwise, and 1 is counterclockwise.
Cgcontextaddarc (CTX, 0, 2*M_PI, 0); Add a circle
Cgcontextdrawpath (CTX, Kcgpathstroke); Draw Path
[Super Drawrect:rect];
}
Do you remember the formula for this circle? Do you still know what M_PI is? equals how much? Hurry up and fix it!
Draw a circle and fill the color
Copy Code code as follows:
Uicolor *acolor = [Uicolor colorwithred:1 green:0.0 blue:0 alpha:1];
Cgcontextsetfillcolorwithcolor (context, acolor.cgcolor);/Fill Color
Cgcontextsetlinewidth (context, 3.0);//width of the line
Cgcontextaddarc (context, 0, 2 * m_pi, 0); Add a circle
Kcgpathfill fills non 0 rule, Kcgpatheofill is represented by parity rule, Kcgpathstroke path, Kcgpathfillstroke path fill, Kcgpatheofillstroke represents stroke, not padding
Cgcontextdrawpath (context, kcgpathfillstroke); Draw Path Plus fill
Draw a rectangle
Copy Code code as follows:
-(void) DrawRect: (cgrect) rect
{
Get current artboard
Cgcontextref CTX = Uigraphicsgetcurrentcontext ();
Color
Cgcontextsetrgbstrokecolor (CTX, 0.2, 0.2, 0.2, 1.0);
The width of the drawing line
Cgcontextsetlinewidth (CTX, 0.25);
Cgcontextaddrect (CTX, CGRectMake (2, 2, 30, 30));
Cgcontextstrokepath (CTX);
[Super Drawrect:rect];
}
Draw Fan
Copy Code code as follows:
Draw a fan, also draw a circle, just set the angle of size, forming a fan
Acolor = [Uicolor colorwithred:0 green:1 blue:1 alpha:1];
Cgcontextsetfillcolorwithcolor (context, acolor.cgcolor);/Fill Color
Draw a 10 radius around the center of the circle to specify the angle sector
Cgcontextmovetopoint (context, 160, 180);
Cgcontextaddarc (context, 160, 180, -60 * pi/180, -120 * pi/180, 1);
Cgcontextclosepath (context);
Cgcontextdrawpath (context, kcgpathfillstroke); Draw Path
To draw a Bezier curve
Copy Code code as follows:
Two times curve
Cgcontextmovetopoint (context, 120, 300);//Set the starting point of the path
Cgcontextaddquadcurvetopoint (context,190, 310, 120, 390);//Set control point coordinates and endpoint coordinates for Bezier curves
Cgcontextstrokepath (context);
Three times curve function
Cgcontextmovetopoint (context, 200, 300);//Set the starting point of the path
Cgcontextaddcurvetopoint (context,250, 280, 250, 400, 280, 300);//Set the control point coordinate and control point coordinate of the Bezier curve
Cgcontextstrokepath (context);