Created
December 10, 2017 10:02
-
-
Save Stickerbox/26fd219cc28a5029216cd5641571f72c 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
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