Skip to content

Instantly share code, notes, and snippets.

@churabou
Created March 23, 2019 07:16
Show Gist options
  • Save churabou/ed0e21596d27a6ca671567c98c58082e to your computer and use it in GitHub Desktop.
Save churabou/ed0e21596d27a6ca671567c98c58082e to your computer and use it in GitHub Desktop.
loadingCircleView
final class LoadingCircleView: UIView {
private var shapeLayer: CAShapeLayer!
override func draw(_ rect: CGRect) {
let center = CGPoint(x: bounds.width / 2, y: bounds.height / 2)
let trackLayer = createTrackLayer(center: center, radius: 100, lineWidth: 10, strokeColor: .darkGray)
layer.addSublayer(trackLayer)
shapeLayer = createTrackLayer(center: center, radius: 100, lineWidth: 10, strokeColor: .orange)
shapeLayer.strokeEnd = 0
layer.addSublayer(shapeLayer)
}
private func createTrackLayer(center: CGPoint,
radius: CGFloat,
lineWidth: CGFloat,
strokeColor: UIColor) -> CAShapeLayer {
let circlePath = UIBezierPath(arcCenter: center,
radius: radius,
startAngle: -.pi / 2,
endAngle: .pi * 3 / 2,
clockwise: true)
let layer = CAShapeLayer()
layer.path = circlePath.cgPath
layer.fillColor = UIColor.clear.cgColor
layer.strokeColor = strokeColor.cgColor
layer.lineWidth = lineWidth
layer.lineCap = .round
return layer
}
func updateValue(_ value: CGFloat) {
let anim = CABasicAnimation(keyPath: "strokeEnd")
guard let fromValue = shapeLayer.presentation()?.value(forKey: "strokeEnd") as? CGFloat else {
return
}
anim.fromValue = fromValue
anim.toValue = value
anim.duration = 1
anim.isRemovedOnCompletion = false
anim.fillMode = .forwards
shapeLayer.add(anim, forKey: "key")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment