- (void)drawRect:(CGRect)rect; Drawrect: when is drawrect called? Drawrect: when is drawrect called? Drawrect: when is drawrect called 2) Set the contentMode property to UIViewContentModeRedraw. DrawRect is automatically called every time a frame is set or changed. 3) This method is called after sizeToFit is called, so sizeToFit can be called first to calculate size. Then the system automatically calls drawRect: method 4) when redrawing. How to redraw 1) Call setNeedsDisplay of a view object that needs to be redrawn 2) call setNeedsDisplayInRect of a View object that needs to be redrawn :(CGRect) Rect: is the place that needs to be redrawn 5. Drawrect = drawRect; drawRect = drawRect; drawRect = drawRect; drawRect = drawRect; drawRect = drawRectCopy the code
Step 1: Core Graphics
1. Get the context 2. Create a path, concatenate a path, and add a path to the context 3. Render, is to display the path in the context 4. Release the path can refer to C draw a line, if you want to go to see other examplesCopy the code
Two, some basic graphics exercises
1. Draw a straight line C
/ / 1. Get the context CGContextRef CTX = UIGraphicsGetCurrentContext (); CGMutablePathRef path = CGPathCreateMutable(); CGPathMoveToPoint(path, **NULL**, 0, 0); CGPathAddLineToPoint(path, **NULL**, 50, 50); //3. Add the path to the context CGContextAddPath(CTX, path); //4. render context CGContextStrokePath(CTX); // release path // CGPathRelease(path); CFRelease(path); // Any object can be releasedCopy the code
2. The OC draw a straight line
UIBezierPath *path=[UIBezierPath bezierPath]; [path moveToPoint:CGPointMake(0, 0)]; [path addLineToPoint:CGPointMake(50, 50)]; // render [path stroke];Copy the code
3. The rectangular
/ / oc form create rectangle UIBezierPath * path = [UIBezierPath bezierPathWithRect: CGRectMake (100, 100, 100, 100)]. [path stroke]; CGContextAddRect(CTX, CGRectMake(100, 100, 100, 100));Copy the code
Rounded rectangle
UIBezierPath *path= [UIBezierPath bezierPathWithRoundedRect:CGRectMake(100, 100, 100, 100) cornerRadius:10];
[path stroke];
Copy the code
5. Ellipse
/ / oc draw ellipse, UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect: CGRectMake (0, 0, 200, 100)]. [path stroke]; / / c draw ellipse CGContextRef CTX = UIGraphicsGetCurrentContext (); CGContextAddEllipseInRect(ctx, CGRectMake(0, 0, 200, 100)); CGContextStrokePath(ctx);Copy the code
6. Circles (by drawing arcs)
/* oc---- Center of the circle, the radius, the starting position, the end position, whether clockwise * / UIBezierPath * path = [UIBezierPath bezierPathWithArcCenter: CGPointMake (50, 5) radius:40.0 startAngle:0 endAngle:M_PI... **YES**] [path stroke]; / / c -- -- -- -- -- the second method draw circles CGContextRef CTX = UIGraphicsGetCurrentContext (); // Get CGContextAddArc(CTX, 70, 50, 50, 0, M_PI, 1); // Context-add path CGContextStrokePath(CTX); // Render contextCopy the code
7. Others, set line width, line head style, set the style of line segment connection
//oc---- draw a bold line UIBezierPath *path1 = [[UIBezierPath alloc] init]; // Create context and add path [path1 moveToPoint:CGPointMake(20, 20)]; [path1 addLineToPoint:CGPointMake(100, 80)]; [path1 addLineToPoint:CGPointMake(180, 20)]; [path1 setLineWidth:10]; / / set the line width [path1 setLineJoinStyle: kCGLineJoinRound]; / / set the style of the joint [path1 setLineCapStyle: kCGLineCapRound]; // Set the header and tail stylesCopy the code
8. Rendering
CGContextDrawPath(CGContextRef CTX, CGPathDrawingMode mode); CGContextDrawPath(CGContextRef CTX, CGPathDrawingMode mode); Used to render the way, please see the following graph / / mo DE type 1. Access to the current context CGContextRef CTX = UIGraphicsGetCurrentContext (); //2. Create path, concatenate path, and add path to context CGContextMoveToPoint(CTX, 50, 50); CGContextAddLineToPoint(ctx, 100, 100); CGContextAddLineToPoint(ctx, 150, 50); CGContextClosePath(ctx); // Close the path CGContextSetLineWidth(CTX, 10); // Set line width [[UIColor redColor] setFill]; // color [[UIColor blueColor] setStroke; Render CGContextDrawPath(CTX, kCGPathFillStroke);Copy the code
UIBezierPath *path = [[UIBezierPath alloc] init]; UIBezierPath *path = [UIBezierPath alloc] init]; [path moveToPoint:CGPointMake(50, 50)]; [path addLineToPoint:CGPointMake(100, 100)]; [path addLineToPoint:CGPointMake(150, 50)]; [path addLineToPoint:CGPointMake(50, 50)]; [path closePath]; [path setLineWidth:10]; [[UIColor greenColor] setFill]; [[UIColor blackColor] setStroke]; [path stroke]; [path fill];Copy the code