The graphics context is either a graphical or a canvas, and we can do the painting on it, and after the drawing is done, put the canvas in our view and view as a picture frame.
Cgcontextref is powerful, and we can draw all kinds of graphics with it. The flexibility to use these techniques during development can help us provide code levels.
Speaking of drawing, I immediately thought: My mathematical formula is almost forgotten.
1. Write text
-(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 (ten, ten, +) Withfont:font]; [Super Drawrect:rect]; }
2. Draw a straight line
-(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]; }
3. Draw a Circle
-(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 for dot coordinates, radius radius, startangle for the beginning of radians, endangle for the end of radians, 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]; }
4. Draw a Rectangle
-(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]; }
IOS Cgcontextref using a brief tutorial