Search code examples
iosswiftcore-graphicsuibezierpath

Setting line cap style doesn't work UIBezierPath


This is my code:

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

I want to round the end of the line, but it's still square, how should I do it?


Solution

  • just tested on PlayGround, Hope it will help

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

    Result

    enter image description here