Iphone QuartzCore-向多边形添加圆角

Iphone QuartzCore-向多边形添加圆角,iphone,ios,core-graphics,quartz-graphics,quartz-core,Iphone,Ios,Core Graphics,Quartz Graphics,Quartz Core,我试图画一个半透明的黑色矩形,中间有一个三角形 我有这个完美的工作与下面的代码。不过,我想把三角形的角弄圆 我尝试了几种不同的添加圆角的技术,例如为笔划设置CGContextSetLineCap,使用quartz创建路径并使用CGContextAddArcToPoint,但没有任何效果 正如我提到的,下面的代码适用于三角形剪切。我怎样才能把三角形的角弄圆 CGContextRef context = UIGraphicsGetCurrentContext(); CGPoint position

我试图画一个半透明的黑色矩形,中间有一个三角形

我有这个完美的工作与下面的代码。不过,我想把三角形的角弄圆

我尝试了几种不同的添加圆角的技术,例如为笔划设置CGContextSetLineCap,使用quartz创建路径并使用CGContextAddArcToPoint,但没有任何效果

正如我提到的,下面的代码适用于三角形剪切。我怎样才能把三角形的角弄圆

CGContextRef context = UIGraphicsGetCurrentContext();
CGPoint position = CGPointMake(rect.size.width/2, rect.size.height * .7);
CGSize size = CGSizeMake(rect.size.width*.8, rect.size.height*.5);

CGFloat firstPointX = (position.x - (size.width / 2));
CGFloat firstPointY = (position.y - (size.height));
CGPoint firstPoint = CGPointMake(firstPointX, firstPointY);

CGFloat secondPointX = position.x + (size.width / 2);
CGFloat secondPointY = position.y - (size.height);
CGPoint secondPoint = CGPointMake(secondPointX, secondPointY);


UIBezierPath *path = [UIBezierPath bezierPath];
[path moveToPoint:position];
[path addLineToPoint:firstPoint];
[path moveToPoint:firstPoint];
[path addLineToPoint:secondPoint];
[path addLineToPoint:position];
[path closePath];

CGContextAddRect(context, rect);
CGContextAddPath(context, path.CGPath);
CGContextEOClip(context);

UIColor *translucentColor = [UIColor colorWithWhite:0 alpha:0.5];
CGContextSetFillColorWithColor(context, translucentColor.CGColor);

CGContextFillRect(context, rect);

UIGraphicsEndImageContext();
编辑:下面是一个我试图完成的例子


我想您应该使用CGContextAddArc。如有必要,它会用一条直线画出圆的一部分。下面是绘制填充UIView的圆形框的代码。对于三角形,你应该有3条线而不是4条。两个在顶部,一个在中间的底部。这是我拥有的roundBoxView类:

CGContextRef context = UIGraphicsGetCurrentContext();
CGRect boxRect = self.bounds;

float bRadius = self.cornerRadius;
float shrink = self.strokeThickness/2;

CGContextBeginPath(context);

// instead of this gray you could later add options for other colors
CGContextSetGrayFillColor(context, 0.0f, self.backgroundOpacity);
CGContextMoveToPoint(context, CGRectGetMinX(boxRect)+shrink + bRadius, CGRectGetMinY(boxRect)+shrink);

CGContextAddArc(context, CGRectGetMaxX(boxRect)-shrink - bRadius, CGRectGetMinY(boxRect)+shrink + bRadius, bRadius, 3 * (float)M_PI / 2, 0, 0);
CGContextAddArc(context, CGRectGetMaxX(boxRect)-shrink - bRadius, CGRectGetMaxY(boxRect)-shrink - bRadius, bRadius, 0, (float)M_PI / 2, 0);
CGContextAddArc(context, CGRectGetMinX(boxRect)+shrink + bRadius, CGRectGetMaxY(boxRect)-shrink - bRadius, bRadius, (float)M_PI / 2, (float)M_PI, 0);
CGContextAddArc(context, CGRectGetMinX(boxRect)+shrink + bRadius, CGRectGetMinY(boxRect)+shrink + bRadius, bRadius, (float)M_PI, 3 * (float)M_PI / 2, 0);

CGContextClosePath(context);
CGContextFillPath(context);

当然,您可以将boxRect作为完整边界传递给您的方法,并使用您想要的任何边界绘制它,而不是将其作为完整边界。要使它成为一个三角形,你只需要三条线而不是两条线,你可能需要做一些数学来计算出起始角和结束角。在长方体上,这些角度始终为90度(此处给出的是令人讨厌的弧度),但在三角形上,您必须动态计算角度,或者在三角形上具有预设的纵横比,以便可以使用预设的开始和停止角度。在如图所示的3个相等角度的示例中,您将执行120度或M_PI/1.5大小的步数。

好-我删除了以前的答案,因为我尝试过,似乎无法使其与剪辑一起工作。如果您在路径上划一条圆线,则可以很好地连接。现在是凌晨4点。我明天再试试。你试过CGContextAddArc吗?