UIBezierPath 笔触在 touchesMoved 中多次重绘

UIBezierPath stroke redraws it multiple times in touchesMoved

我正在 Swift 3.0 中实现一个着色应用程序,它使用 UIBezierPath 触摸来填充着色样式。在 touchesBegan 我创建路径:

touchFillPath = UIBezierPath()

touchesMoved 中,我为每个新点划线:

touchFillPath.addLine(to: location)
touchFillPath.stroke()

但这会导致路径在同一区域上方多次描边,因此所选颜色的不透明度会发生变化。

我需要为每个 touchesMoved 调用绘制路径,以允许用户在移动触摸时看到彩色区域。

如何在不多次覆盖同一区域的情况下多次描边同一路径?

touchFillPath.stroke() 之后,您需要重置路径:

touchFillPath.removeAllPoints()
touchFillPath.move(to: location)

@joeybladb 我试过你的解决方案,但它为每个 'touchesMoved' 动作绘制了小段。

所以为了解决这个问题,我将所有触摸的点保存在一个数组中:

pathPoints.append(NSValue(cgPoint:location))

并在调用 touchFillPath.removeAllPoints() 之后,我再次将所有这些点添加到路径中:

for (index, element) in (pathPoints.enumerated())! {
   let point = element.cgPointValue
   if index == 0 {
       touchFillPath.move(to: point)
   } else {
       touchFillPath.addLine(to: point)
   }
}

以便下次我调用 touchFillPath.stroke() 时,它会遍历整个路径:

touchFillPath.move(to: lastPoint)
touchFillPath.addLine(to: location)
touchFillPath.stroke()