Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通过核心图形绘制自定义形状的圆角_Ios_Core Graphics_Cgcontext - Fatal编程技术网

Ios 通过核心图形绘制自定义形状的圆角

Ios 通过核心图形绘制自定义形状的圆角,ios,core-graphics,cgcontext,Ios,Core Graphics,Cgcontext,我画自定义形状使用核心图形,我想使圆角为这个形状 这是我绘制自定义形状的代码 CGPoint p1=[self getPointFromAngleQuarter:start_angle2 andRaduis:card.small_Raduis andCenter:center]; CGContextMoveToPoint(context, p1.x, p1.y); CGPoint p2=[self getPointFromAngleQuarter:start_angle2 andCenter:c

我画自定义形状使用核心图形,我想使圆角为这个形状 这是我绘制自定义形状的代码

CGPoint p1=[self getPointFromAngleQuarter:start_angle2 andRaduis:card.small_Raduis andCenter:center];
CGContextMoveToPoint(context, p1.x, p1.y);
CGPoint p2=[self getPointFromAngleQuarter:start_angle2 andCenter:center andRaduis:self.large_Raduis];
CGContextAddLineToPoint(context, p2.x, p2.y);
CGContextAddArc(context,center.x, center.y, selectedLargeRaduis, start, end,0);
CGPoint p5=[self getPointFromAngle:end_Angle andCenter:center andRaduis:self.small_Raduis];
CGContextAddLineToPoint(context, p5.x, p5.y);
CGContextAddArc(context,center.x, center.y,selectedSmallRaduis, end, start,1);
CGContextDrawPath(context, kCGPathFill);
这是我自定义形状的最终结果

CGPoint p1=[self getPointFromAngleQuarter:start_angle2 andRaduis:card.small_Raduis andCenter:center];
CGContextMoveToPoint(context, p1.x, p1.y);
CGPoint p2=[self getPointFromAngleQuarter:start_angle2 andCenter:center andRaduis:self.large_Raduis];
CGContextAddLineToPoint(context, p2.x, p2.y);
CGContextAddArc(context,center.x, center.y, selectedLargeRaduis, start, end,0);
CGPoint p5=[self getPointFromAngle:end_Angle andCenter:center andRaduis:self.small_Raduis];
CGContextAddLineToPoint(context, p5.x, p5.y);
CGContextAddArc(context,center.x, center.y,selectedSmallRaduis, end, start,1);
CGContextDrawPath(context, kCGPathFill);
:


如果此形状是纯色,则简单的解决方案是使用非常宽的线宽,再加上圆线帽和圆线连接。不过,我想你希望这个圆形的形状完全位于你照片中包含的形状之内。然后,技巧是将绘制的圆弧偏移等于路径的角半径的量,并以角半径宽度的两倍笔划直线

例如,考虑此图,它不是所需的形状,但向我们展示了如何达到此目的:

背景中的黑色形状是您的原始形状。白色的路径是我将要绘制的实现圆角的路径。浅灰色是指具有大线宽、圆形线条连接和圆形线条帽的路径。深灰色是另一种颜色填充的路径

希望这能说明这个想法。创建一条新路径,偏移拐角半径,并以两倍于拐角半径的线宽绘制。如果您只需使用实心回划替换上图中的浅灰色,并使用实心黑色填充替换上图中的深灰色来绘制新路径,则可以获得所需的形状:

下面是获取Objective-C中我的第一张图像中的白线路径的例行程序:

- (UIBezierPath *)arcWithRoundedCornerAt:(CGPoint)center
                              startAngle:(CGFloat)startAngle
                                endAngle:(CGFloat)endAngle
                             innerRadius:(CGFloat)innerRadius
                             outerRadius:(CGFloat)outerRadius
                            cornerRadius:(CGFloat)cornerRadius {
    CGFloat innerTheta = asin(cornerRadius / 2.0 / (innerRadius + cornerRadius)) * 2.0;
    CGFloat outerTheta = asin(cornerRadius / 2.0 / (outerRadius - cornerRadius)) * 2.0;

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path addArcWithCenter:center
                    radius:innerRadius + cornerRadius
                startAngle:endAngle - innerTheta
                  endAngle:startAngle + innerTheta
                 clockwise:false];

    [path addArcWithCenter:center
                    radius:outerRadius - cornerRadius
                startAngle:startAngle + outerTheta
                  endAngle:endAngle - outerTheta
                 clockwise:true];

    [path closePath];

    return path;
}
或在Swift 3中:

private func arcWithRoundedCorners(at center: CGPoint, startAngle: CGFloat, endAngle: CGFloat, innerRadius: CGFloat, outerRadius: CGFloat, cornerRadius: CGFloat) -> UIBezierPath {
    let innerTheta = asin(cornerRadius / 2 / (innerRadius + cornerRadius)) * 2
    let outerTheta = asin(cornerRadius / 2 / (outerRadius - cornerRadius)) * 2

    let path = UIBezierPath()

    path.addArc(withCenter: center, radius: innerRadius + cornerRadius, startAngle: endAngle - innerTheta, endAngle: startAngle + innerTheta, clockwise: false)
    path.addArc(withCenter: center, radius: outerRadius - cornerRadius, startAngle: startAngle + outerTheta, endAngle: endAngle - outerTheta, clockwise: true)
    path.close()

    return path
}
如果需要,可以通过核心图形调用执行上述操作,但我通常使用UIBezierPath


但是,如果您需要填充的颜色与笔划的颜色不同,那么这个过程就更复杂了,因为您不能仅仅使用这种技术。实际上,您必须定义一条路径,该路径是上述形状的轮廓,但不仅包括绘制两条大圆弧,还包括为每个角绘制四条小圆弧。构建这条路径是一个乏味但简单的三角法,但除非你必须这样做,否则我不会去做。

圆弧会更复杂一些,但除此之外,答案:应该给你一些guidance@DavidRönnqvist你能帮我画一个与圆弧和直线相切的圆角吗?你知道实际的问题是什么吗?@matt我的问题是为这个形状画一个圆角。这个答案应该比它有更多的投票权。谢谢你@Rob