devios1
devios1

Reputation: 37985

Temporarily disable one gesture recognizer while another is firing

I have a UITableView that I want to scroll itself naturally. However I have my own gesture recognizer attached to it as well that is recognized simulataneously (via returning YES from gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:).

After a certain point computed by my gesture handler, I want to temporarily stop the table view from handling the gesture changes of its own underlying gesture. This is because at this point I am resizing the table view and I don't want it to scroll while this is happening (but remain scrolled to the position it was in when the resizing started to take effect).

I've tried recording the other gesture recognizer in gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:, which seems to identify the right one as it is showing up as UIScrollViewPanGestureRecognizer. However nothing I do to that recognizer inside my own handler seems to have any effect.

One would think that setting otherRecognizer.enabled = NO should do the trick, but it doesn't have any effect.

Perhaps the table view is receiving its gesture event before mine, such that disabling it at that point has no effect, but even then you would think that it would be disabled on the next change event, but that's not the case either.

I'm stumped. How do I temporarily block the table view from receiving/processing gestures without canceling the gesture entirely?

Update:

When I log otherRecognizer, it appears as an object of type UIScrollViewPanGestureRecognizer, however its state is Failed. I also notice that there are in fact many calls to gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: (even multiple ones of type UIScrollViewPanGestureRecognizer), so perhaps I am simply recording and disabling the wrong one... but how do I figure out which is the one I want?

Update 2:

If I set tableView.panGestureRecognizer.enabled = NO at the appropriate moment inside my own gesture handler, it get's me half way there—the table no longer scrolls once I start resizing it. However, setting it back to enabled does not re-activate the gesture and it no longer handles any further scrolling until a new gesture starts.

Update 3:

I ended up "solving" this by manually adjusting the scroll view's content offset during the gesture. It seems to work fine, but I'm still curious for the real answer.

Upvotes: 5

Views: 1265

Answers (1)

matt
matt

Reputation: 534885

A UITableView is a UIScrollView. A UIScrollView has a panGestureRecognizer property. That's the gesture recognizer that responds to the user moving a finger on the table view.

However, this has nothing to do with the scroll position changing while you're resizing the table view. The user isn't doing anything - you are. So there is no gesture and nothing to recognize. If you want to control what the scroll view is doing, you need to control it directly. You can use UITableView methods, UIScrollView methods, and the UIScrollView delegate.

It's hard to advise further without a clearer problem statement, but it seems to me that the answer to question you did ask is that you're barking up the wrong tree. But perhaps not; perhaps you are gesturing to resize the table view and you didn't make this clear in the question? In that case, try disabling the panGestureRecognizer. Or, even better, just temporarily turn off all user interaction for the table view and detect this gesture elsewhere.

Upvotes: 1

Related Questions