1

my problem is that touchesBegan() is not called as I know it from other projects. Therefore there must be a difference somewhere to my other projects. Now I got the idea, that the following code prevents calling touchesBegan():

In didMove()

tapRec.addTarget(self, action:#selector(GameScene.tappedView(_:) ))
    tapRec.numberOfTouchesRequired = 1
    tapRec.numberOfTapsRequired = 1
    self.view!.addGestureRecognizer(tapRec)

The function:

@objc func tappedView(_ sender:UITapGestureRecognizer) {

    let point:CGPoint = sender.location(in: self.view)

    print("Single tap")

    print(point)

}

touchesBegan()

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {

        let locationUser = touch.location(in: self)

        if self.atPoint(locationUser) == background {
            print("background touch")
        }
    }
}

I come to the idea that the above function tappedView() is to blame for not executing touchesBegan(), since this function is called when the screen is touched instead of touchesBegan().

Is there a way to use both touchesBegan() and tappedView() without them blocking each other?

Thank you in advance.

Tim-Krs
  • 53
  • 5
  • depends if you set the `cancelsTouchesInView` property of the gesture – Knight0fDragon May 22 '19 at 16:12
  • I'm still not getting anywhere. What value must `cancelsTouchesInView` take for touchesBegan to work again? – Tim-Krs Jun 02 '19 at 07:19
  • I suggest you check out the answers here https://stackoverflow.com/questions/5222998/uigesturerecognizer-blocks-subview-for-handling-touch-events – 0x141E Jun 06 '19 at 08:39

0 Answers0