【问题标题】:Can I set a lag time in between touchesBegan in Swift?我可以在 Swift 中的 touchesBegan 之间设置延迟时间吗?
【发布时间】:2020-11-28 04:45:33
【问题描述】:

我的应用有多个球,但一次只能移动。下面的代码显示,在 touchesBegan 我设置了在 touchesMoved 中使用的特定字段。在 touchesCancelledtouchesEnded 中,我重置了所有内容。

这是我的第一个 Swift 应用程序,但不是我的第一个程序,我知道用户喜欢尝试崩溃应用程序。因此,我用两根手指尽可能快地触摸多个球,最终,是的,在第一个球被设置为“移动”球之前,我在另一个球上运行了 touchesBegan。 是否有任何延迟属性可以让 iOS 在 touchesBegan 之间等待 0.3 秒?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
    guard let touch = touches.first else { return }
    let location = touch.location(in: self)
    let touchedNodes = self.nodes(at: location)
    for node in  touchedNodes{
        if let theNode = node as? MyBall {
            if theNode.nodeType == .ball, theNode.isMoving == false,
                (myGV.currentBall == nil) {
                    theNode.isMoving = true
                    theNode.zPosition += 1
                    myGV.currentBall = theNode
                }
            }
        }
      }

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
    guard touches.first != nil else { return }
    if let touch = touches.first, let node = myGV.currentBall, node.isMoving == true {
        let touchLocation = touch.location(in: self)
        node.position = touchLocation
        node.isMoving = true
        node.inSlot = false
    }
}

【问题讨论】:

  • 你想在 0.3 秒内禁用所有触摸吗?
  • @StefanOvomate 不,只是开始

标签: swift sprite-kit


【解决方案1】:

我认为自然的事情是围绕一个单独的主动触摸来组织它。在touchesMovedtouchesEndedtouchesCancelled 中,在没有活动触摸时忽略所有内容,并忽略任何不是活动触摸的触摸。在touchesBegan 中,如果已经有活动的触摸,则忽略新的触摸。最后,在touchesBegan 中,如果没有主动触球并且触球在球上,则该触球成为主动触球并且该球成为被移动球。草图:

// activeTouch is a UITouch?

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
  for touch in touches {
    if activeTouch == nil {
      // Look for a touched ball; if found set that as the moving ball
      // and set this touch as activeTouch
    }
  }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?){
  guard let active = activeTouch else { return }
  for touch in touches {
    if touch == active {
      // Do stuff with the moving ball
    }
  }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?){
  guard let active = activeTouch else { return }
  for touch in touches {
    if touch == active {
      // The moving ball stops moving, reset activeTouch to nil
    }
  }
}

// Probably touchesCancelled should be the same as touchesEnded, or
// maybe you'd want to move the ball back to the starting position;
// depends on your game.

【讨论】:

  • 我以为我正在这样做。感谢您的简化模板,我发现了我搞砸的地方。谢谢
猜你喜欢
  • 2014-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 2015-12-04
  • 1970-01-01
  • 2013-07-26
相关资源
最近更新 更多