Skip to content

Instantly share code, notes, and snippets.

@strzempa
Created October 11, 2019 08:09
Show Gist options
  • Save strzempa/aeba877ffa8f936f892295d14d44b199 to your computer and use it in GitHub Desktop.
Save strzempa/aeba877ffa8f936f892295d14d44b199 to your computer and use it in GitHub Desktop.
import UIKit
class CircleProgressView: UIView {
var value: CGFloat = 0 {
didSet {
DispatchQueue.main.async { [weak self] in
self?.animate()
}
}
}
private let shapeLayer = CAShapeLayer()
private let trackLayer = CAShapeLayer()
private let lineWidth: CGFloat = 20
override func draw(_ rect: CGRect) {
super.draw(rect)
addShapes()
}
func addShapes() {
let circularPath = UIBezierPath(arcCenter: .zero,
radius: frame.width / 2,
startAngle: 0,
endAngle: CGFloat.pi * 2,
clockwise: true)
trackLayer.path = circularPath.cgPath
trackLayer.strokeColor = UIColor.gray.cgColor
trackLayer.lineWidth = lineWidth
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.lineCap = CAShapeLayerLineCap.round
trackLayer.position = center
shapeLayer.path = circularPath.cgPath
shapeLayer.strokeColor = UIColor.blue.cgColor
shapeLayer.lineWidth = lineWidth
shapeLayer.fillColor = UIColor.clear.cgColor
shapeLayer.lineCap = .round
shapeLayer.strokeEnd = 0
shapeLayer.position = center
shapeLayer.transform = CATransform3DMakeRotation(-CGFloat.pi / 2, 0, 0, 1)
layer.addSublayer(trackLayer)
layer.addSublayer(shapeLayer)
}
func animate() {
let basicAnimation = CABasicAnimation(keyPath: "strokeEnd")
basicAnimation.beginTime = CACurrentMediaTime() + 0.1
basicAnimation.toValue = value
basicAnimation.duration = 1
basicAnimation.fillMode = .forwards
basicAnimation.isRemovedOnCompletion = false
shapeLayer.add(basicAnimation, forKey: "")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment