如何使线条边缘平滑?

How to make the line edge smooth?

我正在尝试绘制光标并且我已经使用 UIBezierPath 来完成此操作。

这是我所做的:

  1. 从顶部指针到右边缘画线。

  2. 从顶部指针到左边缘画线。

  3. 将 bezierPath 设置为具有宽度的图层。

代码如下:

cursorLayerPathPointTop = UIBezierPath()
cursorLayerPathPointTop.lineJoinStyle = CGLineJoin.Round
cursorLayerPathPointTop.lineCapStyle = CGLineCap.Round
cursorLayerPathPointTop.lineWidth = 20

cursorLayerPathPointTop.moveToPoint(cursor_point_top)
cursorLayerPathPointTop.addLineToPoint(cursorRightPoint)
cursorLayerPathPointTop.moveToPoint(cursor_point_top)
cursorLayerPathPointTop.addLineToPoint(cursorLeftPoint)

//adding calyer
cursorLayer = CAShapeLayer()
cursorLayer.lineWidth = 3.0;
cursorLayer.path = cursorLayerPathPointTop.CGPath
cursorLayer.strokeColor = UIColor.whiteColor().CGColor
self.layer.addSublayer(cursorLayer)

我需要把光标加粗,所以设置的原因cursorLayer.lineWidth = 3.0;

但这是我得到的:

正如您所看到的指针,线条没有平滑地连接在一起。我应该怎么做才能解决这个问题?

UIBezierPath 上的 lineJoinStylelineCapStyle 属性仅在使用 UIBezierPath 绘制路径时使用。

你想要 CAShapeLayer 等价物:

cursorLayer.lineJoin = kCALineJoinRound;
cursorLayer.lineCap = kCALineCapRound;

关于您需要绘制单线路径而不是两条线的说法也是正确的。

您的代码当前正在创建两个单独的线段。

而是这样做...

cursorLayerPathPointTop.moveToPoint(cursorRightPoint)
cursorLayerPathPointTop.addLineToPoint(cursor_point_top)
cursorLayerPathPointTop.addLineToPoint(cursorLeftPoint)

这将创建一条线,从右边的点开始,到最高点,然后向下到左边的点。

然后它将在拐角处正确使用连接样式。