设置线帽样式不起作用 UIBezierPath

Setting line cap style doesn't work UIBezierPath

这是我的代码:

    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()

我想把线的末端弄圆,但还是方的,怎么办?

line cap样式配置行结束的样式。你有封闭的路径,即你没有行结束。

您可能正在寻找 line join 样式,它会影响路径的所有 "corners" 或 "vertexes"。

或者,如果您只想直行,请不要关闭路径。否则你会得到两条线段:一条从起点到终点,另一条回到起点。

刚在 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 4.1 更新 Umair Afzal 的代码:

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()