Skip to content

Instantly share code, notes, and snippets.

@almaleh
Last active January 19, 2019 18:10
Show Gist options
  • Save almaleh/2f94fdd6f17d691ff32d0bcb792a2dd6 to your computer and use it in GitHub Desktop.
Save almaleh/2f94fdd6f17d691ff32d0bcb792a2dd6 to your computer and use it in GitHub Desktop.
override func draw(_ layer: CALayer, in ctx: CGContext) {
// 1
let drawingLayer = self.drawingLayer ?? CAShapeLayer()
// 2
drawingLayer.contentsScale = UIScreen.main.scale
// 3
let linePath = UIBezierPath()
// 4
for (index, point) in line.enumerated() {
if index == 0 {
linePath.move(to: point)
} else {
linePath.addLine(to: point)
}
}
drawingLayer.path = linePath.cgPath
drawingLayer.opacity = 1
drawingLayer.lineWidth = lineWidth
drawingLayer.lineCap = .round
drawingLayer.fillColor = UIColor.clear.cgColor
drawingLayer.strokeColor = lineColor.cgColor
// 5
if self.drawingLayer == nil {
self.drawingLayer = drawingLayer
layer.addSublayer(drawingLayer)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment