Skip to content

Instantly share code, notes, and snippets.

@Zedd0202
Created January 31, 2020 07:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Zedd0202/308b9a3f86056cc61ec20dad3c43b8e8 to your computer and use it in GitHub Desktop.
Save Zedd0202/308b9a3f86056cc61ec20dad3c43b8e8 to your computer and use it in GitHub Desktop.
class GradientCircle: UIView, CAAnimationDelegate {
let gradientLayer: CAGradientLayer = CAGradientLayer()
var startAngle: CGFloat = (-(.pi) / 2)
var endAngle: CGFloat = 3 * ((.pi) / 2)
override func draw(_ rect: CGRect) {
let center = CGPoint(x: rect.midX, y: rect.midY)
let animation = CABasicAnimation(keyPath: "strokeEnd")
self.gradientLayer.colors = [UIColor.blue.cgColor, UIColor.cyan.cgColor]
self.gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.5)
self.gradientLayer.endPoint = CGPoint(x: 0.0, y: 0)
self.gradientLayer.type = .conic
self.gradientLayer.frame = rect
self.layer.addSublayer(self.gradientLayer)
animation.fromValue = 0
animation.toValue = 1
animation.duration = 1
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeOut)
animation.delegate = self
let path = UIBezierPath(arcCenter: center, radius: 60, startAngle: startAngle, endAngle: endAngle - (.pi / 2), clockwise: true)
let sliceLayer = CAShapeLayer()
sliceLayer.path = path.cgPath
sliceLayer.fillColor = nil
sliceLayer.lineCap = .round
sliceLayer.strokeColor = UIColor.black.cgColor
sliceLayer.lineWidth = 20
sliceLayer.strokeEnd = 1
sliceLayer.add(animation, forKey: animation.keyPath)
self.layer.mask = sliceLayer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment