首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >擦除UIBezierPath

擦除UIBezierPath
EN

Stack Overflow用户
提问于 2022-01-08 09:41:10
回答 1查看 274关注 0票数 0

我是徒手画一个UIBezierPath,但现在我想能够擦除我以前画的线。有人对如何做到这一点有什么建议吗?

代码语言:javascript
复制
@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
    }
}

我就是这样画线的,到目前为止,为了擦除,我试着:

代码语言:javascript
复制
path.stroke(with: shouldErase ? .clear : .normal, alpha: 1.0)

但当shouldErase是真的时,它似乎什么也不做。

编辑:这就是我想要达到的目标:

https://imgur.com/a/XVfQita

请随意建议任何一种方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-11 09:13:51

我设法用CGContext解决了这个问题。

代码语言:javascript
复制
@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()
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70631155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档