设置线段端点样式无效UIBezierPath

7
这是我的代码:

    let cross = UIBezierPath()
    cross.move(to: CGPoint(x: skull.bounds.maxX, y: skull.bounds.minY))
    cross.addLine(to: CGPoint(x: skull.bounds.minX, y: skull.bounds.maxY))
    cross.close()
    UIColor.red.set()
    cross.lineWidth = 3.0
    cross.lineCapStyle = .round
    cross.stroke()

我想将线条的末端变成圆形,但它仍然是方形的,我应该怎么做?

1
我认为你的线条被剪掉了,因为你使用了最大帧来绘制线条,请将最大值和最小值减少5,看看会发生什么。 - iphonic
3个回答

11

刚在PlayGround上进行了测试,希望它能有所帮助。

let cross = UIBezierPath()
cross.moveToPoint(CGPoint(x: 10, y: 100)) // your point
cross.addLineToPoint(CGPoint(x: 100, y: 10)) // your point
cross.closePath()
cross.lineWidth = 23.0
cross.lineJoinStyle = .Round
cross.stroke()

Swift 5.0

let cross = UIBezierPath()
cross.move(to: CGPoint(x: 10, y: 100)) // your point
cross.addLine(to: CGPoint(x: 100, y: 10)) // your point
cross.close()
cross.lineWidth = 23.0
cross.lineJoinStyle = .round
cross.stroke()

结果

enter image description here


6

线帽样式配置线条末端的样式。如果您有闭合路径,则没有线条末端。

您可能正在寻找连接线样式,它会影响路径的所有“拐角”或“顶点”。

或者,如果您只想要一条直线,请勿关闭路径。否则,您将获得两条线段:从起点到终点的一条线段和另一条线段返回起点。


6

对Umair Afzal的代码进行了Swift 4.1更新:

let cross = UIBezierPath()
cross.move(to: CGPoint(x: 10, y: 100)) // your point
cross.addLine(to: CGPoint(x: 100, y: 10)) // your point
cross.lineWidth = 12
cross.lineCapStyle = .round
cross.stroke()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接