Guest User

Untitled

a guest
Oct 23rd, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. override func draw(_ rect: CGRect) {
  2. let context: CGContext = UIGraphicsGetCurrentContext()!
  3. drawLinearGradient(context)
  4. drawCenterCircle(context)
  5. let circle: CAShapeLayer = drawStroke()
  6. let pathAnimation = CABasicAnimation(keyPath: "strokeEnd")
  7. pathAnimation.duration = percentage
  8. pathAnimation.toValue = percentage
  9. pathAnimation.isRemovedOnCompletion = rendered ? false : (percentageLabel > 20 ? false : true)
  10. pathAnimation.isAdditive = true
  11. pathAnimation.fillMode = kCAFillModeForwards
  12. circle.add(pathAnimation, forKey: "strokeEnd")
  13. }
  14.  
  15. private func drawLinearGradient(_ context: CGContext) {
  16. let circlePoint: CGRect = CGRect(x: 0, y: 0,
  17. width: bounds.size.width, height: bounds.size.height)
  18. context.addEllipse(in: circlePoint)
  19. context.clip()
  20.  
  21. let colorSpace: CGColorSpace = CGColorSpaceCreateDeviceRGB()
  22. let colorArray = [colors.0.cgColor, colors.1.cgColor]
  23. let colorLocations: [CGFloat] = [0.0, 1.0]
  24. let gradient = CGGradient(colorsSpace: colorSpace, colors: colorArray as CFArray, locations: colorLocations)
  25.  
  26. let startPoint = CGPoint.zero
  27. let endPoint = CGPoint(x: 0, y: self.bounds.height)
  28. context.drawLinearGradient(gradient!,
  29. start: startPoint,
  30. end: endPoint,
  31. options: CGGradientDrawingOptions.drawsAfterEndLocation)
  32.  
  33.  
  34. }
  35.  
  36. private func drawCenterCircle(_ context: CGContext) {
  37. let circlePoint: CGRect = CGRect(x: arcWidth, y: arcWidth,
  38. width: bounds.size.width-arcWidth*2,
  39. height: bounds.size.height-arcWidth*2)
  40.  
  41. context.addEllipse(in: circlePoint)
  42. UIColor.white.setFill()
  43. context.fillPath()
  44. }
  45.  
  46. private func drawStroke() -> CAShapeLayer {
  47. let circle = CAShapeLayer()
  48. self.layer.addSublayer(circle)
  49. return circle
  50. }
Add Comment
Please, Sign In to add comment