Skip to content

Instantly share code, notes, and snippets.

@yfujiki
Last active January 17, 2020 12:34
Show Gist options
  • Save yfujiki/b8379832d9224dfa13125441c088992f to your computer and use it in GitHub Desktop.
Save yfujiki/b8379832d9224dfa13125441c088992f to your computer and use it in GitHub Desktop.
override func draw(_ rect: CGRect) {
super.draw(rect)
}
override func draw(_ layer: CALayer, in context: CGContext) {
super.draw(layer, in: context)
// 1. Let the subviews to render into the context
foregroundView?.layer.render(in: context)
// 2. Iterate through line collection and add lines into context
for line in lineCollection {
for (index, point) in line.enumerated() {
if index == 0 {
context.move(to: point)
} else {
context.addLine(to: point)
}
}
}
// 3. Specify the blend mode. This is the key for the scratch card UI. Otherwise, it will just draw path on top.
context.setBlendMode(.clear)
context.setLineWidth(lineWidth)
context.setLineCap(.round)
context.setLineJoin(.round)
// 4. Stroke path. This makes the rendering to happen
context.strokePath()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment