【发布时间】:2021-03-18 09:35:21
【问题描述】:
我正在尝试在 iOS 中实现类似 scratch lottery 的效果。基本效果是使用clear(_rect: CGRect)实现的,但是如何擦除自定义形状而不是矩形呢?
这是我的代码
class ImageMaskView: UIView {
var image = UIImage(named: "maskL")
var line = [CGPoint ]()
override func draw(_ rect: CGRect) {
guard let context = UIGraphicsGetCurrentContext() else {return}
image?.draw(at: CGPoint(x: 0, y: 300))
// erase
line.forEach { (position) in
// print(position)
let rect = CGRect(x: position.x,y:position.y,width:50,height:50)
context.clear(rect)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let position = touches.first?.location(in: nil) else{return }
print(position)
line.append(position)
setNeedsDisplay()
}
}
也许我可以构建一个自定义函数clear?无论如何,如果您能给我一些建议,我将不胜感激
【问题讨论】:
标签: ios swift core-graphics