如何在 CGPathCreateMutable() 之后删除绘制的 CGPath?

How to remove drawn CGPath after CGPathCreateMutable()?

我在 swift 2.0 中使用 SpriteKit 编写了一些简单的行来尝试使用 CGPathCreateMutable() 绘制和创建如下所示的行:

var lineNode = SKShapeNode()
var pathToDraw = CGPathCreateMutable()

 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)
        CGPathMoveToPoint(pathToDraw, nil, location.x, location.y)

        lineNode.path = pathToDraw
        lineNode.strokeColor = SKColor.redColor()
        self.addChild(lineNode)
        }
}

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch in touches {
        let positionInScene = touch.locationInNode(self)

        CGPathAddLineToPoint(pathToDraw, nil, positionInScene.x, positionInScene.y)
        lineNode.path = pathToDraw
        //pathToDraw
    }
}

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    lineNode.removeFromParent()

}

绘制了一条红色路径,然后在我抬起手指后将其删除。我能够删除 lineNode 精灵。但是我无法在绘制后删除路径?绘制第二条线时,我可以看到第一条路径的 'remnant' 。如果我继续绘制应用程序将崩溃。我发现它曾经通过

被删除
 CGPathRelease(pathToDraw)  //in obj-C at least

但后来我发现我不再需要这样做,因为它是一个自主过程?

如何删除它?谢谢!

您永远不会重置路径,在触摸开始回调中您只需添加下一个点。所以要摆脱旧行只需添加

pathToDraw = CGPathCreateMutable()

结束
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

这将为您绘制下一条线提供新的路径