Ios 如何在didMoveToView(Swift、SpriteKit)中注册触摸屏?

Ios 如何在didMoveToView(Swift、SpriteKit)中注册触摸屏?,ios,swift,sprite-kit,Ios,Swift,Sprite Kit,在我的第一个场景中,我有一个重复的动作,我想在用户第一次触摸屏幕时立即停止。是否可以在didMoveToView中检测到触摸?我不能使用TouchsBegind,因为这只是第一次触摸的特例,我不希望每次触摸都重复 override func didMoveToView(view: SKView) { triangle.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) self.addChild(tr

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

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
}
在didMoveToView期间——特别是当你启动应用程序时第一次调用它时——还不能有当前的触摸事件,因为你没有响应主事件循环运行。如果您想处理触摸,TouchesBegind就是这样做的地方

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

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
    }
}