2017-02-18 91 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

實際上,非常感謝你! –