【问题标题】:How to remove drawn CGPath after CGPathCreateMutable()?如何在 CGPathCreateMutable() 之后删除绘制的 CGPath?
【发布时间】:2015-10-23 01:19:23
【问题描述】:

我在 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

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

如何删除它?谢谢!

【问题讨论】:

    标签: sprite-kit cgpath


    【解决方案1】:

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

    pathToDraw = CGPathCreateMutable()
    

    到结尾

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

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

    【讨论】:

    • 谢谢。正是我需要的!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2011-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-08-13
    相关资源
    最近更新 更多