2

I have tried this and this but still no success. Basically I want to create something like this (inner circle). Circle will be created according to some data lets say if data is 50 we will get a half circle and if its 100 we will get full circle.

enter image description here

And this is what I have so far, so how can I create the above design

enter image description here

I have created a View using interface builder, and drawn these circle using this code

override func viewDidLoad() {


    let circlePath = UIBezierPath(arcCenter: CGPoint(x: myView.layer.frame.height/2,y: myView.layer.frame.height/2), radius: CGFloat(100), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

    let shapeLayer = CAShapeLayer()
    shapeLayer.path = circlePath.CGPath
    shapeLayer.fillColor = UIColor.clearColor().CGColor
    shapeLayer.strokeColor = UIColor.grayColor().CGColor
    shapeLayer.lineWidth = 3.0

    let colorCirclePath = UIBezierPath(arcCenter: CGPoint(x: myView.layer.frame.height/2,y: myView.layer.frame.height/2), radius: CGFloat(100), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)

    let coloredShapeLayer = CAShapeLayer()
    coloredShapeLayer.path = colorCirclePath.CGPath
    coloredShapeLayer.fillColor = UIColor.clearColor().CGColor
    coloredShapeLayer.strokeColor = UIColor.whiteColor().CGColor
    coloredShapeLayer.lineWidth = 10.0
    self.myView.layer.addSublayer(coloredShapeLayer)

    self.myView.layer.addSublayer(shapeLayer)



}

This is how I am creating the gradient.

    let gradient: CAGradientLayer = CAGradientLayer()
    let startingColorOfGradient = UIColor(colorLiteralRed: 50/255, green: 189/255, blue: 170/255, alpha: 1.0).CGColor
    let endingColorOFGradient = UIColor(colorLiteralRed: 133/255, green: 210/255, blue: 230/255, alpha: 1.0).CGColor
    gradient.startPoint = CGPoint(x: 1.0, y: 0.5)
    gradient.endPoint = CGPoint(x: 0.0, y: 0.5)
    gradient.colors = [startingColorOfGradient , endingColorOFGradient]
    self.myView.layer.insertSublayer(gradient, atIndex:0)
Community
  • 1
  • 1
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50

1 Answers1

0

If you want a real simulation of gradient color you can check this SO answer .It's based on a cross that divided your rectangular view in 4 portions, then shift the colors on each portion to obtain a regular gradient applied to the layer mask.

Community
  • 1
  • 1
Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133