Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding the edges of a stroke in bezier path circle in swift

I'm trying to draw a circle using UIBezierPath(ovalInRect: CGRectMake()). Then I'll change the strokeEnd property. When this happens, either end of the circular line have sharp corners. Is there any way to round out these corners?

enter image description here(I would like to make the beginning and end of this line curved)

like image 608
Matt Spoon Avatar asked Dec 19 '15 18:12

Matt Spoon


People also ask

How to use Bézier curves in SwiftUI under iOS?

To use them in SwiftUI 2.0 under iOS, you have to create an object called a Shape, a definition that has one required method within it. The code below illustrates a quadratic Bézier curve much like the one you see above. P0 is represented by path.move. P1 is the control point and P2 the to: in the code, aka the final coordinate.

What is a Bézier curve?

In math-speak, a Bézier curve is defined by a set of control points P0 through P n, where n is called its order ( n = 1 for linear, 2 for quadratic, etc.). The formula used is beyond most mere mortals, I fear, but is beautifully illustrated by this animated GIF.

Did Jean-Paul Bézier invent the curve?

You may be surprised to learn that Bézier didn’t invent the curves named after him — he just made them famous.


2 Answers

Assuming you are using a CAShapeLayer to draw this, just set the lineCap, e.g.:

layer.lineCap = .round
like image 82
Rob Avatar answered Nov 19 '22 11:11

Rob


For drawRect based solutions use lineCapStyle

let path = UIBezierPath(...) path.lineCapStyle = .round

like image 43
Lee Irvine Avatar answered Nov 19 '22 12:11

Lee Irvine