http://blog.csdn.net/crayondeng/article/details/11093689#
1.绘制直线
- (void)drawSupernene { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 UIBezierPath *path = [UIBezierPath bezierPath]; CGPoint startP = CGPointMake(10, 10); [path moveToPoint:startP]; [path addLineToPoint:CGPointMake(125, 125)]; [path addLineToPoint:CGPointMake(240, 10)]; // 从路径的终点连接到起点 [path closePath]; // [path addLineToPoint:startP]; // 3.把路径添加到上下文 CGContextAddPath(ctx, path.CGPath); [[UIColor blueColor] setFill]; [[UIColor redColor] setStroke]; CGContextSetLineWidth(ctx, 15); // 4.渲染上下文 // CGContextStrokePath(ctx); // CGContextFillPath(ctx); // 即填充又描边 kCGPathFillStroke CGContextDrawPath(ctx, kCGPathFillStroke); }
2.绘制矩形区域
- (void)drawRectangle { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(10, 10, 200, 200)]; path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(10, 10, 200, 200) cornerRadius:20]; // 3.把路径添加到上下文 CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文 CGContextStrokePath(ctx); }
3.绘制圆弧
- (void)drawCircle { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 10, 200, 100)]; // 3.把路径添加到上下文 CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文 CGContextStrokePath(ctx); }
4.绘制椭圆
- (void)drawArc { // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 CGPoint center = CGPointMake(125, 125); CGFloat radius = 100; CGFloat startA = 0; CGFloat endA = M_PI_2; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; // 3.把路径添加到上下文 CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文 CGContextStrokePath(ctx); }
5.绘制圆形
- (void)drawRect:(CGRect)rect { // Drawing code // 1.获取上下文 CGContextRef ctx = UIGraphicsGetCurrentContext(); // 2.拼接路径 CGPoint center = CGPointMake(125, 125); CGFloat radius = 100; CGFloat startA = 0; CGFloat endA = M_PI_2; UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:startA endAngle:endA clockwise:YES]; [path addLineToPoint:center]; // 3.把路径添加到上下文 CGContextAddPath(ctx, path.CGPath); // 4.渲染上下文 // CGContextStrokePath(ctx); CGContextFillPath(ctx); }
浙公网安备 33010602011771号