Skip to content

Instantly share code, notes, and snippets.

@tranhieutt
Created July 22, 2017 11:38
Show Gist options
  • Save tranhieutt/9cd58de477971a8e95c475944972fab4 to your computer and use it in GitHub Desktop.
Save tranhieutt/9cd58de477971a8e95c475944972fab4 to your computer and use it in GitHub Desktop.
Swift_CG_UIBezierPath
override func draw(_ rect: CGRect) {
// Add ARCs
self.addCirle(innerCircleRadius, capRadius: 20, color: self.firstColor)
self.addCirle(middleCircleRadius, capRadius: 20, color: self.secondColor)
self.addCirle(outerCircleRadius, capRadius: 20, color: self.thirdColor)
}
func addCirle(_ arcRadius: CGFloat, capRadius: CGFloat, color: UIColor) {
let X = self.bounds.midX
let Y = self.bounds.midY
// Bottom Oval
let pathBottom = UIBezierPath(ovalIn: CGRect(x: (X - (arcRadius/2)), y: (Y - (arcRadius/2)), width: arcRadius, height: arcRadius)).cgPath
self.addOval(20.0, path: pathBottom, strokeStart: 0, strokeEnd: 0.5, strokeColor: color, fillColor: UIColor.clear, shadowRadius: 0, shadowOpacity: 0, shadowOffsset: CGSize.zero)
// Middle Cap
let pathMiddle = UIBezierPath(ovalIn: CGRect(x: (X - (capRadius/2)) - (arcRadius/2), y: (Y - (capRadius/2)), width: capRadius, height: capRadius)).cgPath
self.addOval(0.0, path: pathMiddle, strokeStart: 0, strokeEnd: 1.0, strokeColor: color, fillColor: color, shadowRadius: 5.0, shadowOpacity: 0.5, shadowOffsset: CGSize.zero)
// Top Oval
let pathTop = UIBezierPath(ovalIn: CGRect(x: (X - (arcRadius/2)), y: (Y - (arcRadius/2)), width: arcRadius, height: arcRadius)).cgPath
self.addOval(20.0, path: pathTop, strokeStart: 0.5, strokeEnd: 1.0, strokeColor: color, fillColor: UIColor.clear, shadowRadius: 0, shadowOpacity: 0, shadowOffsset: CGSize.zero)
}
func addOval(_ lineWidth: CGFloat, path: CGPath, strokeStart: CGFloat, strokeEnd: CGFloat, strokeColor: UIColor, fillColor: UIColor, shadowRadius: CGFloat, shadowOpacity: Float, shadowOffsset: CGSize) {
let arc = CAShapeLayer()
arc.lineWidth = lineWidth
arc.path = path
arc.strokeStart = strokeStart
arc.strokeEnd = strokeEnd
arc.strokeColor = strokeColor.cgColor
arc.fillColor = fillColor.cgColor
arc.shadowColor = UIColor.black.cgColor
arc.shadowRadius = shadowRadius
arc.shadowOpacity = shadowOpacity
arc.shadowOffset = shadowOffsset
layer.addSublayer(arc)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment