Swift 4:Opacity不断重叠一次(Drawing App)

时间:2017-10-26 01:30:49

标签: swift core-graphics

不透明度在一个笔划中重叠,而不是一个固体不透明笔划。Opacity Stroke

我已经尝试修改了一些上下文,甚至删除了一些,但是没有帮助(我已经把它恢复到原来的状态)。我的目标是尝试使用标记工具进行绘制,并且不透明度大约为0.5。与不透明度为1的铅笔工具相比。当我使用标记时,我得到的只是图中所示的恒定点,我相信它们都是单独的笔划,即使只用一次滑动而不抬起我的手指。

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

这是我的核心图形代码。

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()

    }

1 个答案:

答案 0 :(得分:0)

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

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