2

I'm trying to add a swipe gesture recognizer to my view. Here's a snippet of code from my viewDidLoad()method of the main view controller file:

self.view.addGestureRecognizer(UISwipeGestureRecognizer(target: self, action: #selector(self.swipeHandler(_:))))
self.view.isUserInteractionEnabled = true

And here's the handler method I wrote below the viewDidLoad() function:

@IBAction func swipeHandler(_ gestureRecognizer : UISwipeGestureRecognizer) {
        print("called")
        if gestureRecognizer.state == .ended {
            // Perform action.
            print("ended")
        }
    }

When I swipe, no matter what the direction is, I don't see anything printed to the console.

I think the issue is with the addGestureRecognizerMethod() because when I altered it to a tap recognizer (same exact line, just UITapGestureRecognizer instead of UISwipeGestureRecognizer), I get "called" and "ended" printed in the console.

Any tips?

nicog
  • 102
  • 9

1 Answers1

0

When I've had this happen to me in the past, it is because there was a conflicting gesture recognizer. I had to add this to my viewController:

extension YourViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
HalR
  • 11,411
  • 5
  • 48
  • 80