user2706770 user2706770 - 1 year ago 93
Objective-C Question

tap gesture not recognized on uiimageview

I added two

s, one on another
subview uiview
). In the first view the top
is hidden(
) and in the second view the bottom
is hidden(

Allocating tap gesture:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];

Set user interaction for both uiimageview to YES.

[singleTap setNumberOfTapsRequired:1];
[singleTap1 setNumberOfTapsRequired:1];

// adding gesture to uiimageview

Add tap gesture recognizer and selector respectively.

[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];

But my taps are not recognized.

Can any one tell me where the mistake is?

Answer Source

Try setting setUserInteractionEnabled:YES before adding gesture recognizer.

[imageview1 setUserInteractionEnabled:YES]
[imageview2 setUserInteractionEnabled:YES]

[imageview1 addGestureRecognizer:singleTap];
[imageview2 addGestureRecognizer:singleTap1];   


After the comment you have made I suggest you bring your views to the top before detecting the tap event. Because parent imageView is above and catches these taps.

[yourparentview bringSubviewToFront:imageview1];
[yourparentview bringSubviewToFront:imageview2];