locationswift2uigesturerecognizertouchesbegantouchesended

Why does location not appear in touchesEnded?


I'm looking for the final position of touchesEnded. My touchesBegan code looks like this:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   if let touch = touches.first {
       touchStart = touch.locationInNode(self)
       print("touchStart_1: \(touchStart)")
   }
}

The print gives me the location.

In touchesEnded, I have the following:

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
  if let touch = touches.first, start = touchStart {
    print("touchStart_2: \(touchStart)")
    let location = touch.locationInNode(self)
    print("touchEnded: \(location)")
  }
}

Neither of these prints returns anything. As additional information, I'm using UISwipeGestureRecognizer as well elsewhere but I understand that gesture recognisers work independently of touches. Any idea what's up?


Solution

  • Set this 2 gesture properties :

    let swipeGesturte: UISwipeGestureRecognizer = UISwipeGestureRecognizer(target: self, action: "swipeAction:")
    swipeGesturte.direction = UISwipeGestureRecognizerDirection.Right
    swipeGesturte.cancelsTouchesInView = false // 1.
    swipeGesturte.delaysTouchesEnded = false // 2.
    self.view.addGestureRecognizer(swipeGesturte)
    

    For More about cancelsTouchesInView :What really happens when call setCancelsTouchesInView?