Last active
January 17, 2020 12:34
-
-
Save yfujiki/b8379832d9224dfa13125441c088992f 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
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