iOS绘图系列三:画弧线CGContextAddArc ,CGContextAddArcToPoint,CGContextAddCurveToPoint,UIBezierPath

这一节我们来画弧线,我们需要达到的目标是如图所示


可以用CGContext 去实现

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(ctx, 1, 0, 0, 1);//设置线的颜色
    CGContextSetRGBFillColor(ctx, 0, 0, 1, 1);//设置填充颜色
    CGContextSetLineWidth(ctx, 2); //设置线的宽度
    
    CGContextAddEllipseInRect(ctx, CGRectMake(10, 30, 60, 60)); //画一个椭圆或者圆
    CGContextDrawPath(ctx, kCGPathFillStroke);
    //根据中心点,半径,起始的弧度,最后的弧度,是否顺时针画一个圆弧
    CGContextAddArc(ctx, 140, 60, 30, M_PI/2.f, M_PI, 1);
    CGContextDrawPath(ctx, kCGPathStroke);
    
    CGPoint p[3] =
	{
		CGPointMake(210.0, 30.0),
		CGPointMake(210.0, 60.0),
		CGPointMake(240.0, 60.0),
	};
    //先移到p1点
	CGContextMoveToPoint(ctx, p[0].x, p[0].y);
    //从p1点开始画弧线,圆弧和p1-p2相切;p2-p3和弧线相切,圆弧的半径是20
	CGContextAddArcToPoint(ctx, p[1].x, p[1].y, p[2].x, p[2].y, 20.0);
	CGContextStrokePath(ctx);
    
    //画一个圆角矩形
    CGRect rrect = CGRectMake(210.0, 70.0, 60.0, 60.0);
	CGFloat radius = 10.0;
	CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect);
	CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect);
    CGContextMoveToPoint(ctx, minx, midy);
	CGContextAddArcToPoint(ctx, minx, miny, midx, miny, radius);
	CGContextAddArcToPoint(ctx, maxx, miny, maxx, midy, radius);
	CGContextAddArcToPoint(ctx, maxx, maxy, midx, maxy, radius);
	CGContextAddArcToPoint(ctx, minx, maxy, minx, midy, radius);
	CGContextClosePath(ctx);
	CGContextDrawPath(ctx, kCGPathFillStroke);
    
    //贝塞尔曲线一,两个控制点
    CGPoint s = CGPointMake(30.0, 120.0); //起始点
	CGPoint e = CGPointMake(300.0, 120.0);//终点
	CGPoint cp1 = CGPointMake(120.0, 30.0);//控制点1
	CGPoint cp2 = CGPointMake(210.0, 210.0);//控制点2
	CGContextMoveToPoint(ctx, s.x, s.y);
	CGContextAddCurveToPoint(ctx, cp1.x, cp1.y, cp2.x, cp2.y, e.x, e.y);
	CGContextStrokePath(ctx);
    
    //贝塞尔曲线二,一个控制点
	s = CGPointMake(30.0, 300.0);
	e = CGPointMake(270.0, 300.0);
	cp1 = CGPointMake(150.0, 180.0);
	CGContextMoveToPoint(ctx, s.x, s.y);
	CGContextAddQuadCurveToPoint(ctx, cp1.x, cp1.y, e.x, e.y);
	CGContextStrokePath(ctx);

用UIKit去实现

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(10, 30, 60, 60)];
    [[UIColor redColor] setStroke];
    [[UIColor blueColor] setFill];
    path.lineWidth = 2;
    [path stroke];
    [path fill];
    
    path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(140, 60) radius:30 startAngle:M_PI/2.f endAngle:M_PI clockwise:YES];
    path.lineWidth = 2;
    [path stroke];
    
    path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(210, 70, 60, 60) cornerRadius:5];
    path.lineWidth = 2;
    [path stroke];
    [path fill];
    
    path = [UIBezierPath bezierPath];
    path.lineWidth = 2;
    CGPoint s = CGPointMake(30.0, 120.0);
    CGPoint e = CGPointMake(300.0, 120.0);
    CGPoint cp1 = CGPointMake(120.0, 30.0);
    CGPoint cp2 = CGPointMake(210.0, 210.0);
    [path moveToPoint:s];
    [path addCurveToPoint:e controlPoint1:cp1 controlPoint2:cp2];
    [path stroke];
    
    path = [UIBezierPath bezierPath];
    path.lineWidth = 2;
    s = CGPointMake(30.0, 300.0);
    e = CGPointMake(270.0, 300.0);
    cp1 = CGPointMake(150.0, 180.0);
    [path moveToPoint:s];
    [path addQuadCurveToPoint:e controlPoint:cp1];
    [path stroke];



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值