如何在didMoveToView(Swift,SpriteKit)中注册触摸?

时间:2015-11-13 17:39:22

标签: ios swift sprite-kit

我的第一个场景中有一个重复动作,我想在用户第一次触摸屏幕时立即停止。是否可以在didMoveToView中检测到触摸?我无法使用touchesBegan,因为这只是第一次触摸的特殊情况,我不希望每次触摸都重复这种情况。

override func didMoveToView(view: SKView) {
    triangle.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2)
    self.addChild(triangle)
    triangle.runAction(SKAction.repeatActionForever(rotateAction))
    //->This is where I need to detect a touch
}

1 个答案:

答案 0 :(得分:0)

didMoveToView期间 - 尤其是当您的应用启动时首次调用时 - 由于您未响应主要事件而未能运行,因此无法成为当前触摸事件环。如果您想要触摸,touchesBegan就是这样做的地方。

如果您只想在收到的第一个触摸事件上做某事,您需要做的就是跟踪触摸事件是否是第一个。例如:

var touchedBefore = false
override func touchesBegan(_ touches: Set<UITouch>, withEvent event: UIEvent?) {
    if !touchedBefore {
        touchedBefore = true
        // do your first-touch business
    } else {
        // do your touch handling for other times
    }
}