将笔触颜色改为透明色 CGContext

问题描述 投票:0回答:1

enter image description here我想让用户在自定义UIView上用clearcolor作为笔触颜色。这个代码对于其他颜色都能正常工作,但是对于透明色就不行了。

 override func draw(_ rect: CGRect) {
    guard let context = UIGraphicsGetCurrentContext() else { return }
    context.addRect(rect)
    draw(inContext: context)
}

func draw(inContext context: CGContext) {

    context.setLineWidth(5)
    context.setStrokeColor(UIColor.clear.cgColor)
    context.setLineCap(.round)

    for line in lineArray {
        guard let firstPoint = line.first else { continue }
        context.beginPath()
        context.move(to: firstPoint)
        for point in line.dropFirst() {
            context.addLine(to: point)
        }
        context.strokePath()
    }
}
ios swift cgcontext
1个回答
0
投票

所以,听起来你的情况是这样的。你有一张图片正在图片视图中显示。

enter image description here

而你想让这个图像 隐蔽 直到用户在它上面画画,这时用户的画作应该是 揭示 图像的那部分,像这样。

enter image description here

如果是这样的话,你想要的不是用清晰的颜色来画,你想要的是一个... ... 面罩. 在第二张截图中,实际发生的情况是,我们跟踪用户的画作,并画出了 黑色 渗入 面罩 在图像视图上。

© www.soinside.com 2019 - 2024. All rights reserved.