About IOS quartz2d

Source: Internet
Author: User


quartz2d Introduction (subsequent applications will be relevant)


The first part draws a line



code example:


- (void)drawRect:(CGRect)rect{
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
    
     / / Start drawing
     / / Set the start of the line
     CGContextMoveToPoint(cxContext, 0, 20);
     / / Set the line midpoint
     CGContextAddLineToPoint(cxContext, 100, 20);
     // render
     CGContextStrokePath(cxContext);
}


We only used four lines of code to draw a line in the view, but it would feel very dull, knowledge of a black straight line.


So we'll give him some attributes.


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


Example code:

- (void)drawRect:(CGRect)rect{
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
    
     / / Start drawing
     / / Set the start of the line
     CGContextMoveToPoint(cxContext, 0, 20);
     / / Set the line midpoint
     CGContextAddLineToPoint(cxContext, 100, 20);
     / / Set the color
     CGContextSetRGBStrokeColor(cxContext, 1, 0, 0, 1);
     // render
     CGContextStrokePath(cxContext);
}


Can see him turn red.



To analyze the code I've added, I can guess the width and so on.



I'm adding the width below.



Example code:


- (void)drawRect:(CGRect)rect{
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
    
     / / Start drawing
     / / Set the start of the line
     CGContextMoveToPoint(cxContext, 0, 20);
     / / Set the line midpoint
     CGContextAddLineToPoint(cxContext, 100, 20);
     / / Set the color
     CGContextSetRGBStrokeColor(cxContext, 1, 0, 0, 1);
     / / Set the width
     CGContextSetLineWidth(cxContext, 10);
     // render
     CGContextStrokePath(cxContext);
} 


It's easy to draw a straight line here, but it's not hard to imagine if we're spending more than one test, if we now spend two lines without intersections (we can continue adding lines through Cgcontextaddlinetopoint).


Here's the path, we can draw lines and differentiate them.



Example code:


- (void)drawRect:(CGRect)rect{
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
     / / Create 2 paths
     CGMutablePathRef path1 = CGPathCreateMutable();
     CGMutablePathRef path2 = CGPathCreateMutable();
     / / Start drawing
     / / draw the first line
     CGPathMoveToPoint(path1, NULL, 0, 20);
     CGPathAddLineToPoint(path1, NULL, 100, 20);
     / / draw a 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 because it is CG, so you need to manually release
     CGPathRelease(path1);
     CGPathRelease(path2);
}


Part IIDraw a graphic


Example code:


- (void)drawRect:(CGRect)rect{
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
     / / draw a rectangle
     CGContextAddRect(cxContext, CGRectMake(20, 20, 100, 100));
     // render
     CGContextStrokePath(cxContext);
    
}


Example code:


- (void)drawRect:(CGRect)rect{
    
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
     / / draw a circle
     CGContextAddArc(cxContext, 100, 100, 25, 0, M_PI, 0);
     // render
     CGContextStrokePath(cxContext);
    
}


Part IIIDraw text


Example code:


- (void)drawRect:(CGRect)rect{
     / / Get the graphics context
     CGContextRef cxContext = UIGraphicsGetCurrentContext();
     / / draw a rectangle
     CGContextAddRect(cxContext, CGRectMake(20, 20, 100, 100));
     // render
     CGContextStrokePath(cxContext);
     //Text content
     NSString *str = @"Xu Bao loves to eat fish Xu Bao love to eat fish Xu Bao love to eat fish Xu Bao love to eat fish Xu Bao love to eat fish";
     / / draw the text to the specified area, automatically wrap the line does not display after the range is drawn
     [str drawInRect:CGRectMake(20, 20, 100, 100) withAttributes:nil];
     / / draw the text to the specified point
// [str drawAtPoint:CGPointMake(0, 0) withAttributes:nil];
    
}


Fourth PartDrawing pictures


Instance code:


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


Example 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"];
    
     / / The original map specified position (the top left point of the picture)
     [image drawAtPoint:self.center];
    
} 


About IOS quartz2d


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.