1

I have a situation where I need to have multiple recognisers for the same gesture on the same element in Swift. For context, I've written a UIView extension which adds gesture recognisers for tap and swipe gestures automatically so that occurrences can be tracked easily.

This all worked fine until it was discovered that using my extension on an element that already did something else on a tap event (navigated to a different page) caused the existing tap behaviour to no longer occur.

How can I have multiple UITapGestureRecognisers on the same element and have them all fire, rather than just the one that was added most recently?

I have tried:

public func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
    return true
}

After further research it seems that after my gesture recogniser intercepts the respective event and handles it, this event is not passed on to the UI to be handled normally. This is not due to two conflicting recognisers for the same gesture, but due to my tap recogniser not sending the event on after completion. Does anybody know how to handle a gesture in a recogniser and then pass it on down the chain? Searches and reading about the chain of responders have turned up nothing.

jscs
  • 63,694
  • 13
  • 151
  • 195
Josh Thieme
  • 63
  • 1
  • 5
  • 3
    And you can't just call all of the methods that need to be called from one single tap recognizer action method? – trndjc Mar 18 '18 at 21:58
  • No, that's not possible unfortunately. – Josh Thieme Mar 18 '18 at 22:02
  • 3
    Why on earth not? That's definitely the right way to go. – jscs Mar 18 '18 at 22:04
  • The idea of this extension is to be able to make a call like: `[any ui element].attachTracker(forGestures: [.click, .swipe], ......)` The entire point was avoiding putting tracking calls all over the code or having to add gesture recogniser boilerplate each and every time. The only issue is when attaching a tap recogniser to an element which navigates to another page, my tracking code happens but the page transition no longer does – Josh Thieme Mar 18 '18 at 22:07
  • So you're talking about adding gesture recognizers to views that you're not in control of, like system views. – jscs Mar 18 '18 at 22:11

0 Answers0