自定义UIView的边框绘制不流畅
我正在尝试使用以下代码绘制一个简单的语音气泡:
@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:
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:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了将来的参考,我找到了一种改善边框外观的方法,这将产生以下图片:
我只是使用函数 CGContextSetLineWidth(context, 0.2f) 将边框宽度设置得非常小(我使用了 0.2f)。然后向图层添加阴影,如下所示:
就是这样!
For future reference, I have found a way to improve the look of the border which will result in the following picture:
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:
And that's it!