自定义UIView的边框绘制不流畅

发布于 2024-12-04 03:39:40 字数 2585 浏览 2 评论 0原文

我正在尝试使用以下代码绘制一个简单的语音气泡:

@implementation SpeechBubbleView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

   CGPoint triangleHeadPoint = CGPointMake(0, rect.size.height/2.0);
   float triangleHeight = 5;
   float triangleWidth = 10;

   float maxX = rect.size.width;
   float minX = 0.0;
   float maxY = rect.size.height;
   float minY = 0.0;

   float archTangentLine = 6.0;
   float archRadius = archTangentLine;

   float strokeWidth = 1.0;

   CGContextRef context = UIGraphicsGetCurrentContext();

   CGContextSetLineWidth(context, strokeWidth);   

   CGContextBeginPath(context);

   float currentX = triangleHeadPoint.x;
   float currentY = triangleHeadPoint.y;
   CGContextMoveToPoint(context, currentX, currentY);

   currentX += triangleWidth;
   currentY += (triangleHeight / 2.0);
   CGContextAddLineToPoint(context, currentX, currentY);

   currentY = maxY - archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentY += archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX + archTangentLine, currentY, archRadius);

   currentX = maxX - archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentX += archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX, currentY - archTangentLine, archRadius);

   currentY = minY + archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentY -= archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX - archTangentLine, currentY, archRadius);

   currentX = minX + triangleWidth + archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentX -= archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX, currentY + archTangentLine, archRadius);

   currentY = triangleHeadPoint.y - (triangleHeight / 2.0);
   CGContextAddLineToPoint(context, currentX, currentY);

   CGContextClosePath(context);

   [[UIColor whiteColor] setFill];
   [[UIColor blackColor] setStroke];

   CGContextDrawPath(context, kCGPathFillStroke);
}

@end

此代码的结果如下(不太漂亮)图像:

在此处输入图像描述

绘图并不像我预期的那么平滑,左侧边框比顶部、右侧和底部更宽,并且角比相邻线更宽。

我将填充颜色更改为黑色只是为了检查,结果正如我所期望的:

在此处输入图像描述

为什么边框的宽度可变?有没有办法解决这个问题并使视图周围有平滑的边框?

提前致谢。

I'm trying to draw a simple speech bubble using the following code:

@implementation SpeechBubbleView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
       self.backgroundColor = [UIColor clearColor];
    }
    return self;
}

- (void)drawRect:(CGRect)rect {

   CGPoint triangleHeadPoint = CGPointMake(0, rect.size.height/2.0);
   float triangleHeight = 5;
   float triangleWidth = 10;

   float maxX = rect.size.width;
   float minX = 0.0;
   float maxY = rect.size.height;
   float minY = 0.0;

   float archTangentLine = 6.0;
   float archRadius = archTangentLine;

   float strokeWidth = 1.0;

   CGContextRef context = UIGraphicsGetCurrentContext();

   CGContextSetLineWidth(context, strokeWidth);   

   CGContextBeginPath(context);

   float currentX = triangleHeadPoint.x;
   float currentY = triangleHeadPoint.y;
   CGContextMoveToPoint(context, currentX, currentY);

   currentX += triangleWidth;
   currentY += (triangleHeight / 2.0);
   CGContextAddLineToPoint(context, currentX, currentY);

   currentY = maxY - archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentY += archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX + archTangentLine, currentY, archRadius);

   currentX = maxX - archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentX += archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX, currentY - archTangentLine, archRadius);

   currentY = minY + archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentY -= archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX - archTangentLine, currentY, archRadius);

   currentX = minX + triangleWidth + archTangentLine;
   CGContextAddLineToPoint(context, currentX, currentY);

   currentX -= archTangentLine;
   CGContextAddArcToPoint(context, currentX, currentY, currentX, currentY + archTangentLine, archRadius);

   currentY = triangleHeadPoint.y - (triangleHeight / 2.0);
   CGContextAddLineToPoint(context, currentX, currentY);

   CGContextClosePath(context);

   [[UIColor whiteColor] setFill];
   [[UIColor blackColor] setStroke];

   CGContextDrawPath(context, kCGPathFillStroke);
}

@end

The result of this code is the following (not so pretty) image:

enter image description here

The drawing isn't as smooth as I was expecting it to be, the left side border is wider than the top, right and bottom sides, and the corners are wider than the adjacent lines.

I changed the fill color to black just to check and the result is as I was expecting:

enter image description here

Why does the border have a variable width? Is there a way to fix this and have a smooth border around the view?

Thanks in advance.

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

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

发布评论

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

评论(1

失去的东西太少 2024-12-11 03:39:40

为了将来的参考,我找到了一种改善边框外观的方法,这将产生以下图片:

在此处输入图像描述

我只是使用函数 CGContextSetLineWidth(context, 0.2f) 将边框宽度设置得非常小(我使用了 0.2f)。然后向图层添加阴影,如下所示:

self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.shadowOpacity = 0.8f;
self.layer.shadowRadius = 1.0f;

就是这样!

For future reference, I have found a way to improve the look of the border which will result in the following picture:

enter image description here

I simply made the border width very small (I used 0.2f) using the function CGContextSetLineWidth(context, 0.2f). And then added a shadow to the layer as following:

self.layer.shadowColor = [[UIColor blackColor] CGColor];
self.layer.shadowOffset = CGSizeMake(1.5f, 1.5f);
self.layer.shadowOpacity = 0.8f;
self.layer.shadowRadius = 1.0f;

And that's it!

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