【问题标题】:Erase UIBezierPath擦除 UIBezierPath
【发布时间】: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 为真时,它似乎没有做任何事情。

编辑:这就是我想要实现的目标:

https://imgur.com/a/XVfQita

请随意提出任何方法。

【问题讨论】:

  • 在描边上设置clear,不就是加了一条透明的线,没有做出你想要的效果是正常的吗?在擦除的情况下,在这种情况下使用蒙版怎么样,这会隐藏其他绘图,或者如果你的绘图上有彩色背景,只需使用背景颜色而不是 .clear。
  • 这是不久前的文章,但您可能会发现它有帮助:stackoverflow.com/a/57415125/6257435
  • @Larme 你用蒙版隐藏是什么意思?
  • @LobontAndrei 我觉得 Larme 的意思是你应该只在现有路径的顶部涂上白色或任何背景颜色。所以使用path.stroke(with: shouldErase ? .white : .normal, alpha: 1.0) 可能会解决您的问题。
  • @LobontAndrei - 你查看了我评论中的链接吗?

标签: ios swift uibezierpath


【解决方案1】:

我设法使用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()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2011-12-20
    • 1970-01-01
    • 2012-12-12
    相关资源
    最近更新 更多