擦除 UIBezierPath
Erase UIBezierPath
我正在徒手绘制 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 为 true 时,它似乎什么也没做。
编辑:这是我想要实现的:
随时提出任何一种方法。
我设法使用 CGContext
解决了这个问题。
@objc private func didPanWhiteboard(_ gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began:
drawingState = .began
lastPoint = gesture.location(in: drawingBoardImageView)
case .changed:
drawingState = .moved
let currentPoint = gesture.location(in: drawingBoardImageView)
drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
case .cancelled, .ended:
drawingState = .ended
drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
default: break
}
}
private func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContext(drawingBoardImageView.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingBoardImageView.frame.size.width, height: drawingBoardImageView.frame.size.height))
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(brush.lineCap)
context?.setAlpha(brush.opacity)
context?.setLineWidth(brush.width)
context?.setStrokeColor(brush.color.cgColor)
if shouldErase {
context?.setBlendMode(.clear)
} else {
context?.setBlendMode(.normal)
}
context?.strokePath()
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return }
if drawingState == .ended {
drawingUndoManager.registerForUndo(image)
}
tempImageView.image = image
tempImageView.alpha = 1.0
UIGraphicsEndImageContext()
}
我正在徒手绘制 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 为 true 时,它似乎什么也没做。
编辑:这是我想要实现的:
随时提出任何一种方法。
我设法使用 CGContext
解决了这个问题。
@objc private func didPanWhiteboard(_ gesture: UIPanGestureRecognizer) {
switch gesture.state {
case .began:
drawingState = .began
lastPoint = gesture.location(in: drawingBoardImageView)
case .changed:
drawingState = .moved
let currentPoint = gesture.location(in: drawingBoardImageView)
drawLineFrom(fromPoint: lastPoint, toPoint: currentPoint)
lastPoint = currentPoint
case .cancelled, .ended:
drawingState = .ended
drawLineFrom(fromPoint: lastPoint, toPoint: lastPoint)
default: break
}
}
private func drawLineFrom(fromPoint: CGPoint, toPoint: CGPoint) {
UIGraphicsBeginImageContext(drawingBoardImageView.frame.size)
let context = UIGraphicsGetCurrentContext()
tempImageView.image?.draw(in: CGRect(x: 0, y: 0, width: drawingBoardImageView.frame.size.width, height: drawingBoardImageView.frame.size.height))
context?.move(to: fromPoint)
context?.addLine(to: toPoint)
context?.setLineCap(brush.lineCap)
context?.setAlpha(brush.opacity)
context?.setLineWidth(brush.width)
context?.setStrokeColor(brush.color.cgColor)
if shouldErase {
context?.setBlendMode(.clear)
} else {
context?.setBlendMode(.normal)
}
context?.strokePath()
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return }
if drawingState == .ended {
drawingUndoManager.registerForUndo(image)
}
tempImageView.image = image
tempImageView.alpha = 1.0
UIGraphicsEndImageContext()
}