Does UIGestureRecognizer know which object it is called?

I have a UIGestureRecognizer that I want to work with two different UIViews, both of which are in the same hierarchy of UiViewController views. The action of UIGestureRecognizer is about the same for everyone, so I would like the same function to be called (it will obviously), and I will tell you at runtime what from the UIView I'm dealing with. But how? I do not see that UIGestureRecognizer carries information about the object. Am I missing a line in the documentation or does gestureRecognizer not know to which object it was attached to the one to which it was called? It seems like the language should have known.

Alternatively, perhaps I do not understand 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 that you can:

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

If in fact UIGestureRecognizer does not support simultaneous joining to several objects, then if you know why this does not support this, could you enlighten me? Thanks for the help.

+7
source share
3 answers

A standard is one view for a recognizer. But you can still use one handler method effectively.

You must create an instance of recognizers like this:

 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 might look something like this:

 -(void)handleDblTap:(UITapGestureRecognizer *)tapRec{ if (tapRec.view == viewA){ // double tap view a } else if (tapRec.view == viewB) { // double tap view b } } 
+21
source

You can designate a tag for viewing, and then simply compare this tag and perform an action.

 UITapGestureRecognizer *dblTap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleDblTap:)]; [view addGestureRecognizer: dblTap]; view.tag = 2000; // set any integer 

And when called

 -(void)handleDblTap:(UITapGestureRecognizer *)tapRec{ if (tapRec.view.tag == 2000){ // double tap view with tag } } 
0
source
 UITapGestureRecognizer *dblTapA = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleDblTap:)]; [viewA addGestureRecognizer: dblTapA]; UITapGestureRecognizer *dblTapB = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(handleDblTap:)]; [viewA addGestureRecognizer: dblTapB]; 
-one
source

All Articles