IOS石英气泡带三角形

发布于 2024-12-20 13:37:04 字数 2185 浏览 1 评论 0原文

让石英绘制出我想要的形状时遇到一些困难。基本上我想要这样的形状:

bubble view

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

在此处输入图像描述

感谢您的宝贵时间。

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);

have some trouble getting quartz to draw the shape I want. Basically I am going for a shape like so:

bubble view

I can get the rounded bubble, but when I try to add in the triangle it goes wrong. Here is what I normally get:

enter image description here

Thanks for your time.

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-12-27 13:37:04

看起来我需要做的就是发布一个问题来解决这个问题。 :) 不管怎样,如果有人以后发现这个,这里是我用来让它工作的代码:

 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);

Looks like all I needed to do was post an question to figure it out. :) Anyway here is the code I used to get it working if anyone finds this later down the road:

 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文