Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does UIGestureRecognizer know what object it is called on?

I have a UIGestureRecognizer that I want to work on two different UIViews, both of which are in the same view hierarchy of a UiViewController. The action of the UIGestureRecognizer is about the same on each, so I would like for the same function to be called (it will, obviously) and I will tell at runtime which of the UIViews I am dealing with. But how? I cannot see that the UIGestureRecognizer is carrying the object information with it. Am I missing the line in the documentation or does the gestureRecognizer not know which object that it has been attached to that it is being called on? Seems like the point of the language would be that it would know.

Alternatively, maybe I am misunderstanding the intent of the class and I should not:

UITapGestureRecognizer *dblTap = 
[[UITapGestureRecognizer alloc] initWithTarget: self 
                                        action: @selector(handleDblTap:)];   
[viewA addGestureRecognizer: dblTap]; 
[viewB addGestureRecognizer: dblTap];

and then expect to be able to:

-(void)handleDblTap: (UIGestureRecognizer *)gestureRecognizer
{
     if (viewA)...

If in fact UIGestureRecognizer does not support being attached to multiple objects simultaneously, then, if you know why it does not support this, could you educate me? Thanks for the help.

like image 777
StoneBreaker Avatar asked Apr 24 '12 05:04

StoneBreaker


1 Answers

The standard is one view per recognizer. But you can still efficiently use one handler method.

You would instantiate the recognizers like so:

UITapGestureRecognizer *dblTapViewA = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDblTap:)];   
[viewA addGestureRecognizer: dblTapViewA]; 

UITapGestureRecognizer *dblTapViewB = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDblTap:)];
[viewB addGestureRecognizer: dblTapViewB];

Then your handler method could look something like:

-(void)handleDblTap:(UITapGestureRecognizer *)tapRec{
    if (tapRec.view == viewA){
        // double tap view a
    } else if (tapRec.view == viewB) {
        // double tap view b
    }
}
like image 78
NJones Avatar answered Oct 19 '22 23:10

NJones