如何用UIBezierPath擦除徒手画?

How to use UIBezierPath to erarser the freehand-draw?

我在应用中有两个 UIView ,一个可以在上面徒手绘制调用铅笔视图,另一个放置图形(如图像,矩形等)调用移动视图

现在我用白色擦除铅笔视图,但是移动视图会受到影响(一些区域被覆盖)

我要达到的效果是

这是我的写意代码

 override func drawRect(rect: CGRect) {


        let context = UIGraphicsGetCurrentContext()
        UIGraphicsPushContext(context)

        UIColor.clearColor().setFill()

        if drawType == MouseStatus.erase{
//            UIColor.whiteColor().setStroke()
            UIColor.whiteColor().setStroke();
            self.path.strokeWithBlendMode(kCGBlendModeClear, alpha: 1)
        }else{
            stokeColor.setStroke()
            self.path.strokeWithBlendMode(kCGBlendModeNormal, alpha: 1 )
        }
        self.img.drawInRect(rect)
        self.path.stroke()

    }


    func setStartPoint(point:CGPoint){

        self.path.lineWidth = drawType == MouseStatus.erase ? CGFloat(lineWidth * 3) : CGFloat(lineWidth)
        self.path.moveToPoint(point)
        self.path.addLineToPoint(point)
        self.pointCount = 0
        self.ctr = 0
        self.pts[0] = point

    }

    func midPoint(point1:CGPoint,point2:CGPoint)->CGPoint{
        return CGPointMake((point1.x+point2.x)/2,(point1.y+point2.y)/2)
    }

    func setMovePoint(point:CGPoint){
        self.ctr++
        self.pts[self.ctr] = point
        self.pointCount++

            if self.ctr == 4 {
                self.pts[3] = CGPointMake((self.pts[2].x + self.pts[4].x)/2.0, (self.pts[2].y + self.pts[4].y)/2.0);
                self.path.moveToPoint(self.pts[0])
                self.path.addCurveToPoint(self.pts[3], controlPoint1: self.pts[1], controlPoint2: self.pts[2])
                self.setNeedsDisplayInRect(self.bounds)
                self.pts[0] = self.pts[3];
                self.pts[1] = self.pts[4];
                self.ctr = 1;
            }

    }

    func setEndPoint(){
        if self.ctr != 4 && self.ctr > 1{
            for index in 1...self.ctr {

                self.path.moveToPoint(self.pts[index-1])
                self.path.addQuadCurveToPoint(self.pts[index], controlPoint: self.pts[index])

            }
        }

        self.drawBitmap()

        self.setNeedsDisplay()
        self.path.removeAllPoints()
        self.ctr = 0
    }
     func drawBitmap(){

        UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0);
        if drawType == MouseStatus.erase {
            UIColor.whiteColor().setStroke();
            self.path.strokeWithBlendMode(kCGBlendModeClear, alpha: 1)

        }else{
            stokeColor.setStroke();
            self.path.strokeWithBlendMode(kCGBlendModeNormal, alpha: 1)

        }
        let rectpath:UIBezierPath = UIBezierPath(rect: self.bounds)
        UIColor.clearColor().setFill()
        rectpath.fill();
        self.img.drawAtPoint(CGPointZero)
        self.path.stroke()
        self.img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext()

    }

首先,定义 Bool,或者一些你正在擦除的指示:

let eraserEnabled : Bool = false

启用橡皮擦时,更改 eraserEnabled = true。这将使您保持状态。现在,您可以通过更改混合模式来更改路径,使其实际上 -ERASES-:

// Instead of this
self.path.stroke()

// Do this
self.path.strokeWithBlendMode(erase ? kCGBlendModeClear : kCGBlendModeNormal, alpha: 1.0)

现在当您启用橡皮擦时,您将有效地"cut"清除所有内容,但它将被替换为清晰的颜色,而不是白色,因此您将能够看到背景或其他元素。

希望对您有所帮助!