我如何区分spriteKit和swift中的手势和触摸?

时间:2015-12-14 04:12:22

标签: ios swift sprite-kit

我有两个功能,一个在我触摸场景时被激活而另一个在我做出手势时被激活,但当我做出一个手势时,场景会检测到一个触摸并执行这两个功能

class GameScene: SKScene, SKPhysicsContactDelegate {

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    // physics & collisions
    physicsWorld.gravity = CGVectorMake(0.0, 0.0)
    physicsWorld.contactDelegate = self

    // Swipe down
    let swipeDown:UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: Selector("swipedDown:"))
    swipeDown.direction = .Down
    view.addGestureRecognizer(swipeDown)

}

//MARK: Swipes
func swipedDown(sender:UISwipeGestureRecognizer){
    //first function
    swipeDown()
}

//MARK: Touches
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    /* Called when a touch begins */

    for touch in touches {
        let location = touch.locationInNode(self)
        // second function
        attack()
    }
}

}

2 个答案:

答案 0 :(得分:2)

而不是touchesBegan,请尝试使用点击手势(UITapGestureRecognizer)进行攻击操作。

这不是必需的,但如果您在滑动和点击之间仍然存在冲突,请使用requireGestureRecognizerToFail使点按手势取决于滑动失败,以确保在滑动过程中未检测到点击。< / p>

答案 1 :(得分:1)

你可以试试这样的事情

var swiped = Bool()
func swipedDown(sender:UISwipeGestureRecognizer){
     //first function
     swipeDown()
     swiped = true 

}

Override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */

     for touch in touches {
         let location = touch.locationInNode(self)
         // second function
         if swiped == false {
               attack()
         }
     }
}


 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?)  {
      for touch in touches {
            swiped = false
       }

}

希望这会有所帮助。