UIGestureRecognizer solo funciona en una clase

I have a number of classes and want to add a UIGestureRecognizer to their UIImageViews.

I add one correctly to all the classes. Thing is I duplicate code so that each class is adding the same recognizer. These are local recognizers

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget : self action : @selector (handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[background addGestureRecognizer:singleTap];
[singleTap release];

so it should still work for each class.

The problem is that it only works on the first class, not on the others.

background is the UIImageView and is present in every class. So I have multiple backgrounds.

I present each new ModelViewController class, is this maybe what the issue is?

I use the recognizer as follows

- (void) handleSingleTap : (UIGestureRecognizer*) sender
{
      //do whatever
}

Each class also implements the

@interface Someclass : UIViewController <UIGestureRecognizerDelegate>

Im not sure why it isnt working though. I have a print out in each handleSingleTap method. and nothing gets printed.

preguntado el 08 de enero de 11 a las 18:01

1 Respuestas

Primero Are you adding the mismo recognizer object to different views? If so that won't work. UIGestureRecognizer objects only detect/track gestures in one view. So make sure you have a different recognizer object for each of your views like:

UITapGestureRecognizer *r1 = [UITapGestureRecognizer alloc] initWithTarget:self action:...];
[view1 addGestureRecognizer:r1];
[r1 release];

UITapGestureRecognizer *r2 = [UITapGestureRecognizer alloc] initWithTarget:self action:...];
[view2 addGestureRecognizer:r2];
[r2 release];

...

Segundo Make sure the view (in this case UIImageView) is [uiimageview setUserInteractionEnabled:YES];

Tercero If you are just detecting simple touches then is not necessary to adopt UIGestureRecognizerDelegate protocol, so just delete that protocol from the @interface

Espero eso ayude

Respondido el 09 de enero de 11 a las 09:01

Actually I just needed to add this [someUiImageView setUserInteractionEnabled:YES]; Thanks for the help - some_id

Thanks, nacho4d. That's a really helpful answer - 5 hr

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.