美文网首页
CGContextAddArcToPoint

CGContextAddArcToPoint

作者: 暖羊羊_d603 | 来源:发表于2018-08-09 14:51 被阅读0次

记录一下CGContextAddArcToPoint 的用法:
需要三个点, 连城两条直线, 然后根据中间的夹角画弧,不需要确定圆心以及起始结束位置
下面看看下面的而例子和图形比较容易理解

    CGContextRef context=UIGraphicsGetCurrentContext();
    CGContextSetRGBStrokeColor(context,1,0,0,1);
    CGContextMoveToPoint(context,150,50);
    CGContextAddLineToPoint(context,100,80);
    CGContextAddLineToPoint(context,130,150);
    
    CGContextMoveToPoint(context,150,50);//圆弧的起始点
    CGContextAddArcToPoint(context,100,80,130,150,50);
    CGContextStrokePath(context);
20130926134549046.png
@ 绘制的时候 从p1 -> p2  p2 -> p3 连接的直线 然后在其夹角画弧
@ move 的时候 为p1 
@ addArc 参数中 start:p2, end:p3, 最后结束点为新的p1(即p3作为次下次的p1)

下面的方法 是绘制一个中间透明 四周背景的image 用于在view需要圆角优化的时候 在上面添加一个imageView即可

- (UIImage *)drawRectImageSize:(CGSize)size {
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat radius = 30;
    CGFloat x1,x2,x3,x4,y1,y2,y3,y4;
    x1 = 0;
    x2 = 30;
    x3 = size.width - x2;
    x4 = size.width;
    
    y1 = 0;
    y2 = 30;
    y3 = size.height - y2;
    y4 = size.height;
    
    // 绘制的时候 从p1 -> p2  p2 -> p3 连接的直线 然后在其夹角画弧,
//     move 的时候 为p1 addArc start:p2 end:p3, 最后结束点为新的p1(即p3作为次下次的p1)
    CGContextMoveToPoint(context, x1, y2);
    CGContextAddArcToPoint(context, x1, y1, x2, y1, radius);
    CGContextAddArcToPoint(context, x4, y1, x4, y2, radius);
    CGContextAddArcToPoint(context, x4, y4, x3, y4, radius);
    CGContextAddArcToPoint(context, x1, y4, x1, y3, radius);
    CGContextAddLineToPoint(context, x1, y2);
    
    CGContextSetLineWidth(context, 0);
//    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    
    CGContextSetFillColorWithColor(context, [UIColor greenColor].CGColor);
    
    CGContextDrawPath(UIGraphicsGetCurrentContext(), kCGPathFillStroke);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

效果:其中superView是一个红色的view 上面添加的imageView 背景白色 这样被盖住了下面的


屏幕快照 2018-08-09 下午2.50.57.png

相关文章

网友评论

      本文标题:CGContextAddArcToPoint

      本文链接:https://www.haomeiwen.com/subject/kcoqbftx.html