当用户沿着一条线追踪时,SKShapeNode
路径是从线段(来自底层“瓷砖”)构建的。实际路径是通过锁定用户先前拖过的线,并在最近的线到最新触摸点找到最近点,然后构建如下路径来构建的:
var activatedPointPath: UIBezierPath {
let activatedPointPath = UIBezierPath()
// self.state.activatedPoints contains the end point
// of each full line segment the user has traced through
if let firstActivatedPoint = self.state.activatedPoints.first {
activatedPointPath.move(to: firstActivatedPoint)
for activatedPoint in self.state.activatedPoints {
activatedPointPath.addLine(to: activatedPoint)
}
// self.state.endOfLine contains the last point the user
// has touched, which may or may not be at the end of a line
// segment
if let lastPoint = self.state.endOfLine {
activatedPointPath.addLine(to: lastPoint)
}
}
return activatedPointPath
}
然后将其分配给SKShapeNode:
self.lineNode.path = self.activatedPointPath.cgPath
但是当用户跟踪该行时,之前的段会在绘制到屏幕时闪烁三角形,如下所示:
上面的图片来自一个简单的3x3平铺网格,允许用户追踪一条简单的直线:
这是一个更复杂的例子,该行从左下角开始,到右上角结束:
可能导致这些文物的原因是什么?
编辑,虽然我找到了解决此问题的 解决方案,但我仍然欢迎任何有关原始代码无效和新代码的解释。< / p>
答案 0 :(得分:1)
我对此问题的解决方法是更改activatedPointPath
计算变量以构建组合在一起的线段,而不是一次性绘制线条。
新代码如下所示:
var previousPoint = firstActivatedPoint
for activatedPoint in self.state.activatedPoints {
let newSegment = UIBezierPath()
newSegment.move(to: previousPoint)
newSegment.addLine(to: activatedPoint)
activatedPointPath.append(newSegment)
previousPoint = activatedPoint
}
if let lastPoint = self.state.endOfLine {
let newSegment = UIBezierPath()
newSegment.move(to: previousPoint)
newSegment.addLine(to: lastPoint)
activatedPointPath.append(newSegment)
} else {
print("No last point")
}
现在可以产生流畅的线条。