CGBlendMode.clear不适合我

时间:2016-10-31 21:52:06

标签: ios swift core-graphics

我正在尝试在项目中制作橡皮擦功能。我可以画线,但是当我调用CGBlendMode.clear时没有动作。

任何人都可以看到我做错了什么或错过了什么。我在下面发布了一个代码段,但如果需要另一个,请告诉我。

fileprivate func drawLine(_ stroke: Stroke) -> Void {
        print(#function)
        let properties = stroke.settings
        let array = stroke.points

        if array?.count == 1 {

            let pointStr = array?[0]
            let point = CGPointFromString(pointStr!)
            self.drawLineFrom(point, toPoint: point, properties: properties!)
        }

        for i in 0 ..< (array?.count)! - 1 {
            let pointStr0 = array?[i]
            let pointStr1 = array?[i+1]

            let point0 = CGPointFromString(pointStr0!)
            let point1 = CGPointFromString(pointStr1!)
            self.drawLineFrom(point0, toPoint: point1, properties: properties!)
        }
        self.mergeViews()
    }



    fileprivate func drawLineFrom(_ fromPoint: CGPoint, toPoint: CGPoint, properties: StrokeSettings) -> Void {
        print(#function)

        UIGraphicsBeginImageContext(self.frame.size)
        let context = UIGraphicsGetCurrentContext()

        context!.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
        context!.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))

        context!.setLineCap(CGLineCap.round)
        context!.setLineWidth(properties.width)

        print(#function, "drawEraseSwitch: ", drawEraseSwitch)
        if drawEraseSwitch == 0 {
            context!.setBlendMode(CGBlendMode.normal)
            context!.setStrokeColor(red: properties.color.red, green: properties.color.green, blue: properties.color.blue, alpha: 1.0)
        } else {
            context!.setStrokeColor(red: properties.color.red, green: properties.color.green, blue: properties.color.blue, alpha: 1.0)
            context!.setBlendMode(CGBlendMode.clear)
        }


        context!.strokePath()

        self.backgroundImageView.image?.draw(in: self.backgroundImageView.frame)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        self.backgroundImageView.image = image
        self.backgroundImageView.alpha = properties.color.alpha
        UIGraphicsEndImageContext()

    }





fileprivate func mergeViews() {
    print(#function)
    UIGraphicsBeginImageContext(self.mainImageView.frame.size)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    self.mainImageView.image = image
    UIGraphicsEndImageContext()

    //self.backgroundImageView.image = nil
}

0 个答案:

没有答案