Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 810
Bright Avatar asked Sep 19 '16 08:09

Bright


People also ask

How do I Close a subpath in béZier?

A single Bézier path object can contain any number of open or closed subpaths, where each subpath represents a connected series of path segments. Calling the close () method closes a subpath by adding a straight line segment from the current point to the first point in the subpath.

What is a UIBezierPath object?

A UIBezierPath object combines the geometry of a path with attributes that describe the path during rendering. You set the geometry and attributes separately and can change them independent of one another. After you have the object configured the way you want it, you can tell it to draw itself in the current context.

Can I use béZier path objects multiple times?

Because the creation, configuration, and rendering process are all distinct steps, Bézier path objects can be reused easily in your code. You can even use the same object to render the same shape multiple times, perhaps changing the rendering options between successive drawing calls.

What is the use of a line path?

A path that consists of straight and curved line segments that you can render in your custom views. You use this class initially to specify just the geometry for your path.


Video Answer


2 Answers

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

Result

enter image description here

like image 124
Umair Afzal Avatar answered Oct 23 '22 17:10

Umair Afzal


The line cap style configures the style of the line ending. You have closed path, i.e. you have no line ending.

You are probably looking for the line join style, which affects all "corners" or "vertexes" of a path.

Alternatively, if you just want a straight, don't close the path. Otherwise you get two line segments: one from the starting point to the end point and another one back to the start.

like image 43
Codo Avatar answered Oct 23 '22 15:10

Codo