Lydia Lydia - 11 days ago 8
Swift Question

Drawing shapes using UIBezierPath in swift

I dont know how to draw shape using UIBezierPath.Anyways i tried with lot of ways but did not successes.

I want to make the below shapesenter image description here

Please help

Answer
func drawSliceInView(view: UIView, startAngle: CGFloat, endAngle: CGFloat, radius: CGFloat)
{
    let centerPoint = CGPoint(x: view.frame.size.width / 2, y: view.frame.size.width / 2)
    let startValue = (startAngle * 2 * CGFloat(M_PI)) / 360 - CGFloat(M_PI_2)
    let endValue = (endAngle * 2 * CGFloat(M_PI)) / 360 - CGFloat(M_PI_2)

    let path = UIBezierPath(arcCenter: centerPoint, radius: radius, startAngle: startValue, endAngle: endValue, clockwise: true)
    path.addLineToPoint(centerPoint)
    path.closePath()

    let sliceLayer = CAShapeLayer()
    sliceLayer.path = path.CGPath
    sliceLayer.fillColor = UIColor.blueColor().CGColor
    sliceLayer.backgroundColor = nil
    sliceLayer.strokeColor = UIColor.whiteColor().CGColor
    sliceLayer.lineWidth = 2.0

    view.layer.addSublayer(sliceLayer)
}

Usage example:

drawSliceInView(self.view, startAngle: -30, endAngle: 90, radius: 50)
drawSliceInView(self.view, startAngle: 90, endAngle: 210, radius: 50)