开发者

UIGestureRecognizer only works on One class

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];开发者_JAVA技巧
[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.


First Are you adding the same 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];

...

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

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

Hope it helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜