我可以在 Swift 中的 touchesBegan 之间设置延迟时间吗?
Can I set a lag time in between touchesBegan in Swift?
我的应用程序有多个球,但一次只能移动。我下面的代码显示,在 touchesBegan 处,我设置了 touchesMoved 中使用的特定字段。在 touchesCancelled 和 touchesEnded 中,我重置了所有内容。
这是我的第一个 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
}
}
我认为自然而然的事情是围绕一个单独的主动触摸来组织它。在touchesMoved
、touchesEnded
和touchesCancelled
中,当没有活动触摸时忽略一切,并忽略任何不是活动触摸的触摸。在 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.
我的应用程序有多个球,但一次只能移动。我下面的代码显示,在 touchesBegan 处,我设置了 touchesMoved 中使用的特定字段。在 touchesCancelled 和 touchesEnded 中,我重置了所有内容。
这是我的第一个 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
}
}
我认为自然而然的事情是围绕一个单独的主动触摸来组织它。在touchesMoved
、touchesEnded
和touchesCancelled
中,当没有活动触摸时忽略一切,并忽略任何不是活动触摸的触摸。在 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.