我在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精灵。但是我无法在绘图后删除路径?在绘制第二条线时,我能够看到第一条路径的“残余”。如果我继续绘制应用程序将崩溃。我发现曾经通过
删除它 CGPathRelease(pathToDraw) //in obj-C at least
然后我发现我不再需要这样做,因为它是一个自主过程?
如何删除它?谢谢!
答案 0 :(得分:0)
你永远不会重置路径,并在触摸开始回调你只需添加下一个点。所以要摆脱旧线只需添加
pathToDraw = CGPathCreateMutable()
到
的结尾override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
这将为您提供一个新的路径,可用于下一行绘制