Skip to content

Instantly share code, notes, and snippets.

@Stickerbox
Created December 10, 2017 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Stickerbox/26fd219cc28a5029216cd5641571f72c to your computer and use it in GitHub Desktop.
Save Stickerbox/26fd219cc28a5029216cd5641571f72c to your computer and use it in GitHub Desktop.
extension CAShapeLayer {
func drawCircle(in rect: CGRect, with configuration: PathConfiguration) {
let center = CGPoint(x: rect.maxX / 2, y: rect.maxY / 2)
let longestSide = rect.height < rect.width ? rect.height : rect.width
let circularPath = UIBezierPath(arcCenter: center, radius: (longestSide / 2) - (configuration.lineWidth / 2), startAngle: configuration.startAngle, endAngle: 2 * CGFloat.pi, clockwise: true)
self.path = circularPath.cgPath
self.fillColor = UIColor.clear.cgColor
self.strokeColor = configuration.color.cgColor
self.lineWidth = configuration.lineWidth
self.lineCap = kCALineCapRound
switch configuration.type {
case .progress:
self.strokeEnd = 0
case .track:
self.strokeEnd = 1
case .custom(let value):
self.strokeEnd = value
}
}
}
struct PathConfiguration {
let color: UIColor
let lineWidth: CGFloat
let startAngle: CGFloat
let type: TrackType
}
enum TrackType {
case progress, track
case custom(CGFloat)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment