简体   繁体   English

为什么 SKShapeNode 会撕裂?

[英]Why does SKShapeNode tear?

As a user traces along a line, a SKShapeNode path is built from line segments (which come from underlying 'tiles').当用户沿着一条线进行跟踪时, SKShapeNode路径是由线段(来自底层“瓷砖”)构建的。 The actual path is built by locking in lines the user has previously dragged over, and finding the closest point on the closest line to the latest touch point, then building a path like so:实际路径是通过锁定用户先前拖过的线来构建的,并在最近的线上找到最接近最新触摸点的点,然后构建如下路径:

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
}

this is then assigned to the SKShapeNode as:然后将其分配给 SKShapeNode 为:

self.lineNode.path = self.activatedPointPath.cgPath

however when the user is tracing the line, previous segments flicker with triangles missing while being drawn to the screen, like so:然而,当用户追踪这条线时,之前的线段在被绘制到屏幕时会闪烁,三角形丢失,如下所示:

实线 泪流满面 泪流满面

The images above are from a simple 3x3 tile grid allowing the user to trace a simple straight line:上面的图像来自一个简单的 3x3 瓷砖网格,允许用户跟踪一条简单的直线:

瓷砖的线框

Here is a more complex example, where the line starts on the bottom left and ends at the top right:这是一个更复杂的示例,其中该行从左下角开始,在右上角结束:

带角的更复杂的例子

What could be causing these artifacts?是什么导致了这些文物?

edit while I have found a solution to this issue, I would still welcome any explanation for why the original code didn't work and the new code does.编辑虽然我找到这个问题解决方案,但我仍然欢迎任何解释为什么原始代码不起作用而新代码起作用的原因。

My fix for this issue was to change the activatedPointPath calculated variable to build the line out of segments combined together, rather than drawing the line in one pass.我对这个问题的解决方法是更改activatedPointPath计算变量以构建组合在一起的线段,而不是一次绘制线段。

The new code looked like this:新代码如下所示:

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")
}

which now produces smooth lines.现在产生平滑的线条。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM