2017-02-18 118 views
0

林时试图编写代码,与手指的拖动绘制,但是当手指移开删除(在SpriteKitSwift 3)线节点在SKShapeNode创建另一个新的节点

var shapeNodes = [SKShapeNode]() 
var pathToDraw = CGMutablePath() 
var lineNode = SKShapeNode() 

func deleteAllShapeNodes() { 

    for node in shapeNodes { 
     node.removeFromParent() 
    } 
    shapeNodes.removeAll(keepingCapacity: false) 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches { 
     firstPoint = touch.location(in: self) 
    } 

    shapeNodes.append(lineNode) 
    pathToDraw.move(to: CGPoint(x: firstPoint.x, y: firstPoint.y)) 
    lineNode.lineWidth = 4 
    lineNode.strokeColor = UIColor.white 
    lineNode.name = "Line" 
    lineNode.zPosition = 100000 
    lineNode.path = pathToDraw 
    self.addChild(lineNode) 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches{ 
     positionInScene = touch.location(in: self) 
    } 
    shapeNodes.append(lineNode) 
    pathToDraw.addLine(to: CGPoint(x: positionInScene.x, y: positionInScene.y)) 
    lineNode.path = pathToDraw 
    firstPoint = positionInScene 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    for touch: AnyObject in touches{ 
     TouchEndPosition = touch.location(in: self) 
    } 
    self.deleteAllShapeNodes() 
} 

的重现第一行绘制和删除完美,但是当我开始绘制第二行时,第一行再次出现。

+0

的[CGMutablePath不迅速释放(http://stackoverflow.com/questions/42320529/cgmutablepath-not-releasing-in-swift) – Losiowaty

+0

为什么你可能重复问同样的问题两次? – Losiowaty

回答

0

创建全新的CGMutablePath似乎是获得空的CGMutablePath的唯一方法。

而且您将SKShapeNode(保存在lineNode中)的唯一实例添加到shapeNodes中,这是无意义的。您的lineNode会保留您添加到pathToDraw的所有行。

通常,您可能需要学习何时实例化对象,以及何时将它们释放

试试这个:

//### You are adding the same instance multiple times into this array, it's nonsense. 
//var shapeNodes = [SKShapeNode]() 
//### `pathToDraw` can be kept in the `SKShapeNode`. 
//var pathToDraw = CGMutablePath() 
//### In this case, this is not a good place to instantiate an `SKShapeNode`. 
var lineNode: SKShapeNode? 

func deleteAllShapeNodes() { 
    //### Release the `SKShapeNode` contained in `lineNode` to delete all lines. 
    lineNode?.removeFromParent() 
    lineNode = nil 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let firstPoint = touches.first!.location(in: self) 

    self.lineNode?.removeFromParent() 

    //### If you want somethings which live while dragging, `touchesBegan(_:with:)` is a good place to instantiate them. 
    let lineNode = SKShapeNode() 
    //### Create an empty new `CGMutablePath` at each `touchesBegan(_:with:)`. 
    let pathToDraw = CGMutablePath() 

    self.lineNode = lineNode 
    pathToDraw.move(to: firstPoint) 
    lineNode.lineWidth = 4 
    lineNode.strokeColor = UIColor.white 
    lineNode.name = "Line" 
    lineNode.zPosition = 100000 
    //### `pathToDraw` is kept in the `SKShapeNode`. 
    lineNode.path = pathToDraw 
    self.addChild(lineNode) 
} 

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
    let positionInScene = touches.first!.location(in: self) 

    if let lineNode = self.lineNode { 
     //### Modify the `CGMutablePath` kept in the `SKShapeNode`. 
     let pathToDraw = lineNode.path as! CGMutablePath 
     pathToDraw.addLine(to: positionInScene) 
     //### Refresh the shape of the `SKShapeNode`. 
     lineNode.path = pathToDraw 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    self.deleteAllShapeNodes() 
} 
+0

实际上,非常感谢你! –

相关问题