Swift 4:不透明度在一笔中不断重叠(绘图应用程序)



不透明度在一个笔画中重叠,而不是一个实心不透明笔画。不透明度描边

尝试修改一些上下文,甚至删除了一些上下文,但是这些都没有帮助(我已将其恢复到原始状态)。我的目标是尝试使用标记工具进行绘制,与不透明度为 1 的铅笔工具相比,不透明度约为 .5。当我使用记号笔时,我得到的只是图片中所示的恒定点,我相信即使轻轻一扫而不抬起手指,它们都是单独的笔画。

如果有人对此有任何了解,我真的可以使用一些帮助(:

这是我的核心图形代码。

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    swiped = true
    if let touch = touches.first {
        let currentPoint = touch.preciseLocation(in: self.view)
        drawLines(fromPoint: lastPoint, toPoint: currentPoint)
        lastPoint = currentPoint
        tool.center = currentPoint
    }
}
func drawLines(fromPoint: CGPoint, toPoint: CGPoint) {
    UIGraphicsBeginImageContext(self.view.frame.size)
    imageView.image?.draw(in: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height))
    let context = UIGraphicsGetCurrentContext()
    context?.move(to: CGPoint(x: fromPoint.x, y: fromPoint.y))
    context?.addLine(to: CGPoint(x: toPoint.x, y: toPoint.y))
    context?.setStrokeColor(UIColor(red: red, green: green, blue: blue, alpha: opacityValue).cgColor)
    context?.setBlendMode(CGBlendMode.normal)
    context?.setLineJoin(CGLineJoin.bevel)
    context?.setLineCap(CGLineCap.round)
    context?.setLineWidth(brushSize)
    context?.strokePath()
    imageView.image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if !swiped {
        drawLines(fromPoint: lastPoint, toPoint: currentPoint)
    }
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
    touchesEnded(touches, with: event)
}
    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }

问题是您将每个线段作为单独的线段绘制到图像上。由于每个线段都重叠,并且您使用的是部分透明颜色,因此重叠的透明区域看起来更暗。

一种解决方案是保留所有已绘制的点的数组(而不仅仅是最新和以前的点)。然后构造一条由所有点之间的所有线段组成的路径,然后描边该单个路径。这将消除较暗的重叠区域。

最新更新