如何使线条边缘平滑?

时间:2016-01-19 12:32:07

标签: ios uibezierpath cashapelayer

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

这是我做的:

  1. 从顶部指针到右边缘的绘制线。

  2. 从顶部指针到左边缘的绘制线。

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

  4. 这是代码:

    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;

    的原因

    但这是我得到的:

    enter image description here

    正如您所看到的那样,指针线未顺利连接在一起。我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:1)

lineJoinStyle上的lineCapStyleUIBezierPath属性仅在您使用UIBezierPath绘制路径时使用。

你想要CAShapeLayer等价物:

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

Fogmeister对于你需要画一条线路而不是两条线也是对的。

答案 1 :(得分:0)

您的代码目前正在创建两个单独的细分受众群。

而是这样做......

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

这将从右侧点开始创建一条线,到达顶点,然后向下到左侧点。

然后它会在角落处正确使用连接样式。

相关问题