Created
October 3, 2019 08:52
-
-
Save ntnmrndn/1d2c7367d095a225cf17dbce6285c6d7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CamembertProgressView: UIView { | |
private let camembertFillingLayer = CAShapeLayer() | |
private let lineWidth: CGFloat = 5 | |
private let color = UIColor.white.cgColor | |
var progress: CGFloat = 0 { | |
didSet { | |
camembertFillingLayer.strokeEnd = progress | |
} | |
} | |
override init(frame: CGRect) { | |
super.init(frame: frame) | |
configure() | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
configure() | |
} | |
private func configure() { | |
backgroundColor = .clear | |
let inFrame = bounds.inset(by: UIEdgeInsets(top: lineWidth, left: lineWidth, bottom: lineWidth, right: lineWidth)) | |
let circlePath = UIBezierPath(ovalIn: inFrame) | |
let circleLayer = CAShapeLayer() | |
circleLayer.lineWidth = lineWidth | |
circleLayer.path = circlePath.cgPath | |
circleLayer.strokeColor = color | |
layer.addSublayer(circleLayer) | |
let camembertFillingPath = UIBezierPath(ovalIn: inFrame.applying(CGAffineTransform(scaleX: 0.5, y: 0.5).translatedBy(x: (inFrame.width + lineWidth) / 2, y: (inFrame.width + lineWidth) / 2))) | |
camembertFillingLayer.strokeColor = color | |
camembertFillingLayer.lineWidth = inFrame.width / 2 | |
camembertFillingLayer.path = camembertFillingPath.cgPath | |
camembertFillingLayer.strokeEnd = progress | |
camembertFillingLayer.anchorPoint = .init(x: 0.5, y: 0.5) | |
camembertFillingLayer.frame = bounds | |
camembertFillingLayer.backgroundColor = UIColor.clear.cgColor | |
layer.addSublayer(camembertFillingLayer) | |
camembertFillingLayer.transform = CATransform3DMakeRotation(3 * .pi / 2, 0.0, 0.0, 1.0) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment