Search code examples
iosswiftuitapgesturerecognizertap

UITapGestureRecognizer - Detecting if there's a sprite at tap location (swift)


I'm making an iOS game and need to know how to get the location of tap for uitapgesturerecognizer. I do NOT want to use touchesBegan, touchesEnded, etc. I also need to know how to check if a user double tapped the sprite node. I already have set up uitapgesturerecognizer's all I need to know is how to find the location of touch and see if it is the same location as a sprite node.

Any help is greatly appreciated!


Solution

  • Assuming your sprite is of a class that inherits from UIView (and I think they generally are) the handler function for the gesture recognizer should look like this.

    func handleDoubleTap(gestureRecognizer: UITapGestureRecognizer) {
    
        let tapLocation = gestureRecognizer.location(in: gestureRecognizer.view)
        if mySprite.frame.contains(tapLocation){
            print("tapped")
        }
    
    }