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.

How much do you remember in high school math?

I remember maths was always good when I was in school. So that they dare not to be noisy with others in class. A noisy, others will come to a sentence: Think you are good at maths, you can what about it? I am so helpless to say.

So many years have passed, to tell the truth, high school math formula or something, have forgotten almost. It's harder than writing the code to do the senior-grade exams now.

As a programmer, math is good, it must be an advantage. Take a picture, and if you remember the formulas clearly, you can draw some nice graphics out of the way.

Next, let's see what cgcontextref can achieve.

Write text
- (void)drawRect:(CGRect)rect{    //获得当前画板    CGContextRef ctx = UIGraphicsGetCurrentContext();    //颜色    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);    //画线的宽度    CGContextSetLineWidth(ctx, 0.25);    //开始写字    [@"我是文字" drawInRect:CGRectMake(10, 10, 100, 30) withFont:font];      [super drawRect:rect];}

This code can be very beautiful to write four characters: I am the word. It is easy to understand that every sentence has a comment.

Draw a line
- (void)drawRect:(CGRect)rect{    //获得当前画板    CGContextRef ctx = UIGraphicsGetCurrentContext();    //颜色    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);    //画线的宽度    CGContextSetLineWidth(ctx, 0.25);    //顶部横线    CGContextMoveToPoint(ctx, 0, 10);    CGContextAddLineToPoint(ctx, self.bounds.size.width, 10);    CGContextStrokePath(ctx);    [super drawRect:rect];}
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];} 

Do you remember this round formula? Do you still know what M_PI is? Is it equal to what? Hurry up and mend your mind!

Draw a rectangle
- (void)drawRect:(CGRect)rect{    //获得当前画板    CGContextRef ctx = UIGraphicsGetCurrentContext();    //颜色    CGContextSetRGBStrokeColor(ctx, 0.2, 0.2, 0.2, 1.0);    //画线的宽度    CGContextSetLineWidth(ctx, 0.25);    CGContextAddRect(ctx, CGRectMake(2, 2, 30, 30));    CGContextStrokePath(ctx);    [super drawRect:rect];}

Graphic thousands, original aim. More drawing methods, look forward to your exploration.

Author Li Gang is just online (www.superqq.com) webmaster, Baidu hundred column author
iOS engineer non-famous from media, public number iOS development: Iosdevtip operator


?

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.