IOS Cgcontextref using a brief tutorial

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.