Re: UIGestureRecognizer in superview firing
Re: UIGestureRecognizer in superview firing
- Subject: Re: UIGestureRecognizer in superview firing
- From: Roland King <email@hidden>
- Date: Tue, 03 May 2011 20:19:18 +0800
On 03-May-2011, at 12:30 AM, Matt Neuburg wrote:
> On Mon, 02 May 2011 16:15:59 +0800, Roland King <email@hidden> said:
>> I have a UIView subclass which has a number of UIButtons and UISliders on it, it's a sort of control panel. I added UIPanGestureRecognizer to it with the idea that if you stick your finger down outside one of the embedded controls, you can move the window around. Wrote the handler for the gesture recognizer, that worked.
>>
>> However I found that if I press one of the buttons, or try to drag one of the sliders which are subviews of my view, the PanGestureRecognizer fires. That completely breaks those controls as the sliders stop sliding and you cannot drag yourself off the button to cancel the press, you just drag the view around.
>>
>> I've re-read the UIGestureRecognizer documentation again twice and cannot figure out why this is happening.
>
> This is correct behavior and is very thoroughly explained in my book (Chapter 18, "Touches"). As I say there, you need to think of a view as surrounded by a swarm of gesture recognizers - those attached to the view, those attached to its superview, and so on up the chain. Every gesture recognizer in the swarm gets a crack at recognizing any gesture; if any does, then (by default) all the other gesture recognizers in the swarm are made to fail.
>
> The reason for this behavior is obvious if you consider the case of two views where you put one finger on each view and rotate around their common center. Neither view can detect this gesture, since it is a two-finger gesture and each view knows about only one finger, so the only way this can work is to attach the rotate gesture recognizer to the superview. This in turn implies that touches must be delivered to gesture recognizers up the superview chain.
>
>> I'm going to use the PanGestureRecognizer delegate method gestureRecognizer:shouldReceiveTouch: to stop this happening.
>
> That's correct. It's very easy to implement this, because a touch has a view, so you can detect immediately that this touch was not on the superview. m.
>
That's two questions of mine you've answered in two days Matt, thanks very much.
It makes some sense that gesture recognizers work like that, as you say if the touches weren't passed to the superview recognizers there's no way you could recognize 'macro' gestures, whereas it's quite easy with that one delegate method to turn the behavior off if you don't want it.
That is however exactly NOT what the documentation says, it gives no hint at all that gesture recognizers above the hit-tested view will be tickled nor does it really go into what circumstances you might want to use gestureRecognizer:shouldReceiveTouch:. I agree the actual behavior makes sense so I'll file a documentation bug on that, googling around I'm not the only person confused by this. _______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden