【发布时间】:2022-01-11 17:13:51
【问题描述】:
我正在徒手绘制 UIBezierPath,但现在我希望能够擦除之前绘制的线条。有人对如何做到这一点有任何建议吗?
@objc private func didPanWhiteboard(_ gesture: UIPanGestureRecognizer) {
let location = gesture.location(in: drawCanvasView)
switch gesture.state {
case .began:
path = UIBezierPath()
path.move(to: location)
strokeLayer = CAShapeLayer()
drawCanvasView.layer.addSublayer(strokeLayer)
path.stroke(with: shouldErase ? .clear : .normal, alpha: 1.0)
strokeLayer.strokeColor = toolColor.cgColor
strokeLayer.fillColor = UIColor.clear.cgColor
strokeLayer.lineWidth = toolWidth
strokeLayer.path = path?.cgPath
case .changed:
path.addLine(to: location)
strokeLayer.path = path.cgPath
case .cancelled, .ended: drawLayers.append(strokeLayer)
default: break
}
}
这是我画线的方式,到目前为止我尝试过擦除:
path.stroke(with: shouldErase ? .clear : .normal, alpha: 1.0)
但当 shouldErase 为真时,它似乎没有做任何事情。
编辑:这就是我想要实现的目标:
请随意提出任何方法。
【问题讨论】:
-
在描边上设置clear,不就是加了一条透明的线,没有做出你想要的效果是正常的吗?在擦除的情况下,在这种情况下使用蒙版怎么样,这会隐藏其他绘图,或者如果你的绘图上有彩色背景,只需使用背景颜色而不是 .clear。
-
这是不久前的文章,但您可能会发现它有帮助:stackoverflow.com/a/57415125/6257435
-
@Larme 你用蒙版隐藏是什么意思?
-
@LobontAndrei 我觉得 Larme 的意思是你应该只在现有路径的顶部涂上白色或任何背景颜色。所以使用
path.stroke(with: shouldErase ? .white : .normal, alpha: 1.0)可能会解决您的问题。 -
@LobontAndrei - 你查看了我评论中的链接吗?
标签: ios swift uibezierpath