Introduction to IOS zz2d and iosquartz2d

Source: Internet
Author: User

Introduction to IOS zz2d and iosquartz2d
Quartz2D introduction (relevant applications will be available in the future)

Part 1Draw a straight line

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // start drawing // set the straight line starting point CGContextMoveToPoint (cxContext, 0, 20 ); // set CGContextAddLineToPoint (cxContext, 100, 20) in a straight line; // render CGContextStrokePath (cxContext );}

:

We used only four lines of code to draw a straight line in the view, but it would be boring, just a black line of knowledge.

In this way, we add some attributes to him.

In order to test, I first added only the color to him.

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // start drawing // set the straight line starting point CGContextMoveToPoint (cxContext, 0, 20 ); // set the CGContextAddLineToPoint (cxContext, 100, 20) in the straight line. // set the color CGContextSetRGBStrokeColor (cxContext, 1, 0, 0, 1); // render CGContextStrokePath (cxContext );}

 

:

It turns to red.

After analyzing the code I added, I can guess there are still width and so on.

I will add the following width.

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // start drawing // set the straight line starting point CGContextMoveToPoint (cxContext, 0, 20 ); // set the CGContextAddLineToPoint (cxContext, 100, 20) in the straight line. // set the color CGContextSetRGBStrokeColor (cxContext, 1, 0, 0, 1); // set the width CGContextSetLineWidth (cxContext, 10); // render CGContextStrokePath (cxContext );}

 

:

We can now draw a straight line here, but it is not difficult to think about the multi-test rate. If we are spending two lines without intersections (we can continue to add lines through CGContextAddLineToPoint) how can we differentiate them.

Next we will introduce the path, which can be used to draw lines and differentiate them.

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // create two paths CGMutablePathRef path1 = CGPathCreateMutable (); CGMutablePathRef path2 = CGPathCreateMutable (); // start drawing // draw the first line CGPathMoveToPoint (path1, NULL, 0, 20); CGPathAddLineToPoint (path1, NULL, 100, 20 ); // draw the second line CGPathMoveToPoint (path2, NULL, 0, 50); CGPathAddLineToPoint (path2, NULL, 100, 50); // Add the path to the context CGContextAddPath (cxContext, path1); CGContextAddPath (cxContext, path2); // render CGContextStrokePath (cxContext); // release the CGPathRelease (path1); CGPathRelease (path2 );}

:

Part 2Drawing

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // draw the rectangular CGContextAddRect (cxContext, CGRectMake (20, 20,100,100 )); // render CGContextStrokePath (cxContext );}

:

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // draw the circle CGContextAddArc (cxContext, 100,100, 25, 0, M_PI, 0); // render CGContextStrokePath (cxContext );}

:

Part 3Draw text

Sample Code:

-(Void) drawRect :( CGRect) rect {// obtain the graphic context CGContextRef cxContext = UIGraphicsGetCurrentContext (); // draw the rectangular CGContextAddRect (cxContext, CGRectMake (20, 20,100,100 )); // render CGContextStrokePath (cxContext); // The text content NSString * str = @ "xubao loves fish xubao and fish xubao loves fish xubao "; // draw the text into the specified area and automatically wrap the text out of the range without displaying [str drawInRect: CGRectMake (20, 20,100,100) withAttributes: nil]; // draw the text to the specified vertex // [str drawAtPoint: CGPointMake (0, 0) withAttributes: nil];}

:

Part 4Draw Images

Instance code:

-(Void) drawRect :( CGRect) rect {UIImage * image = [UIImage imageNamed: @ "2.jpg"]; // tiled [image drawAsPatternInRect: self. bounds];}

:

Sample Code:

-(Void) drawRect :( CGRect) rect {UIImage * image = [UIImage imageNamed: @ "2.jpg"]; // stretch [image drawInRect: self. bounds];}

:

Instance code:

-(Void) drawRect :( CGRect) rect {UIImage * image = [UIImage imageNamed: @ "2.jpg"]; // specify the position of the source image (top left of the image) [image drawAtPoint: self. center];}

:

Related Article

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.