Ive wrote a few simple lines in swift 2.0 using SpriteKit to experiment using CGPathCreateMutable() to draw and creating lines shown below:
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()
}
A red path is drawn and then is removed after I lift my finger. I am able to remove the lineNode sprite. However I am not able to remove the path after drawing? When drawing the second line I am able to see the 'remnant' of the first path. If I keep drawing the app will crash. Ive found that it used to be removed by doing
CGPathRelease(pathToDraw) //in obj-C at least
but then Ive found out i no longer have to do this because it is an autonomous process?
How do I remove it? Thanks!
You never reset the path and in the touches began callback you just add the next point. so to get rid of the old line just add
pathToDraw = CGPathCreateMutable()
to the end of
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
This will give you a new path to work with for the next line you draw