I am creating an arc using UIBezierPath arc convenience method. I want the ends of the arc to be rounded off - see attached sketch for exact requirement! The cornerRadius option used in the roundedRect version isn't available for the arc method. Anyone got an ideas on how to achieve this? Thanks in advance. (This is different to previously asked question in that it provides exact requirement)
let center = CGPoint(x:bounds.width/2, y: bounds.height/2)
let radius: CGFloat = max(bounds.width, bounds.height)
let arcWidth: CGFloat = 10
let startAngle: CGFloat = 4.6 / 3 * π
let endAngle: CGFloat = 4.4 / 3 * π
let path = UIBezierPath(arcCenter: center, radius: radius/2 - arcWidth/2, startAngle: startAngle, endAngle: endAngle, clockwise: true)
path.lineWidth = arcWidth
// all thats needed to make the ends rounded is
path.lineCapStyle = .Round
path.stroke()
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.
After configuring the geometry and attributes of a Bézier path, you draw the path in the current graphics context using the stroke () and fill () methods. The stroke () method traces the outline of the path using the current stroke color and the attributes of the Bézier path object.
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.
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 actually does the trick is lineCap property. You can use the code below.
func drawCircle(view: UIView, startingAngle: CGFloat, endAngle: CGFloat) -> CAShapeLayer {
let path = UIBezierPath(arcCenter: view.center, radius: CGFloat((view.bounds.size.height/2) - 10), startAngle: startingAngle, endAngle:endAngle, clockwise: true)
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.CGPath
shapeLayer.strokeColor = UIColor.blackColor().CGColor
shapeLayer.lineWidth = 10.0
shapeLayer.fillColor = UIColor.clearColor().CGColor
shapeLayer.lineCap = kCALineCapRound
view.layer.addSublayer(shapeLayer)
return shapeLayer
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With