让石英绘制我想要的形状有些麻烦。基本上,我要这样的形状:

我可以得到圆角的气泡,但是当我尝试添加三角形时会出错。这是我通常会得到的:

谢谢你的时间。

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

CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, self.BorderWidth);
CGContextSetStrokeColorWithColor(context, self.BorderColor.CGColor);
CGContextSetFillColorWithColor(context, self.FillColor.CGColor);


float pad = BorderWidth + 0.5f;
float width = currentFrame.size.width - BorderWidth - 0.5f;
float height = currentFrame.size.height - BorderWidth - 0.5f;
float rounding = BorderRadius - BorderWidth;

CGContextMoveToPoint(context,pad + TriangleSize.width, pad);

//top
CGContextAddArcToPoint(context,
                       width,
                       pad,
                       width,
                       height,
                       rounding);
//right
CGContextAddArcToPoint(context,
                       width,
                       height,
                       round(width / 2.0f),
                       height,
                       rounding);
//bottom
CGContextAddArcToPoint(context,
                       TriangleSize.width + pad,
                       height,
                       pad,
                       pad ,
                       rounding);

//left
CGContextAddArcToPoint(context,
                       TriangleSize.width,
                       pad + TriangleSize.height*3,
                       width,
                       pad,
                       0);

CGContextAddLineToPoint(context,-TriangleSize.width - pad,TriangleSize.height);
CGContextAddLineToPoint(context,pad + TriangleSize.width, pad + TriangleSize.height);

CGContextAddArcToPoint(context,
                       pad + TriangleSize.width,
                       pad - TriangleSize.height,
                       width,
                       height,
                       rounding);

CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

最佳答案

看来我需要做的只是发布一个问题以解决问题。 :)无论如何,这是我用来使它正常工作的代码,如果以后有人发现它的话:

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

CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineWidth(context, self.BorderWidth);
CGContextSetStrokeColorWithColor(context, self.BorderColor.CGColor);
CGContextSetFillColorWithColor(context, self.FillColor.CGColor);


float pad = BorderWidth + 0.5f;
float width = currentFrame.size.width - BorderWidth - 0.5f;
float height = currentFrame.size.height - BorderWidth - 0.5f;
float rounding = BorderRadius - BorderWidth;
float pos = (height/3); //height/2 //setting this to a third as I want the arrow to be a bit higher than the middle

CGContextMoveToPoint(context,pad*3 + TriangleSize.width, pad);
//top
CGContextAddArcToPoint(context,
                       width,
                       pad,
                       width,
                       height,
                       rounding);
//right
CGContextAddArcToPoint(context,
                       width,
                       height,
                       round(width / 2.0f),
                       height,
                       rounding);
//bottom
CGContextAddArcToPoint(context,
                       pad + TriangleSize.width,
                       height,
                       pad,
                       pad ,
                       rounding);

CGContextAddLineToPoint(context, TriangleSize.width,pos + TriangleSize.height);
CGContextAddLineToPoint(context, 0,pos);
CGContextAddLineToPoint(context, TriangleSize.width,pos - TriangleSize.height);
//left
CGContextAddArcToPoint(context,
                       pad + TriangleSize.width,
                       pad,
                       width,
                       pad,
                       rounding);


CGContextClosePath(context);
CGContextDrawPath(context, kCGPathFillStroke);

// Draw a clipping path for the fill
CGContextBeginPath(context);
CGContextMoveToPoint(context,pad*3 + TriangleSize.width, pad);
CGContextAddArcToPoint(context, width, pad, width, height, rounding);
CGContextAddArcToPoint(context, width, height, round(width / 2.0f), height,rounding);
CGContextAddArcToPoint(context, pad + TriangleSize.width,height, pad, pad, rounding);
CGContextAddLineToPoint(context, TriangleSize.width,pos + TriangleSize.height);
CGContextAddLineToPoint(context, 0,pos);
CGContextAddLineToPoint(context, TriangleSize.width,pos - TriangleSize.height);
CGContextAddArcToPoint(context, pad + TriangleSize.width,pad,width, pad, rounding);

CGContextClosePath(context);
CGContextClip(context);

关于objective-c - iOS三角形 quartz 气泡,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8448751/

10-14 19:36