Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tap gesture not recognized on uiimageview

I added two uiimageviews, one on another subview uiview (imageview1,imageview2). In the first view the top uiimageview is hidden(imageview2) and in the second view the bottom imageview is hidden(imageview1).

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?

like image 473
user2706770 Avatar asked Oct 07 '13 06:10

user2706770


People also ask

How to add tap gesture on UIImageView in Swift?

Open the Library, look for "Tap Gesture Recognizer" object. Drag the object to your storyboard, and set the delegate to the image you want to trigger actions. Then go to the view controller, drag the same object to set the IBAction.

How to add tap gesture recognizer to Uiview?

Adding a Tap Gesture Recognizer in Interface Builder You don't need to switch between the code editor and Interface Builder. Open Main. storyboard and drag a tap gesture recognizer from the Object Library and drop it onto the view we added earlier. The tap gesture recognizer appears in the Document Outline on the left.


2 Answers

Try setting setUserInteractionEnabled:YES before adding gesture recognizer.

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

Update:

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]; 
like image 133
User 1531343 Avatar answered Oct 02 '22 07:10

User 1531343


UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)]; singleTap.numberOfTapsRequired = 1; singleTap.numberOfTouchesRequired = 1; singleTap.delegate = self; [imageview1 addGestureRecogniser:singleTap]; [singleTap1 release];  imageview1.userInteractionEnabled = YES; //disabled by default 
like image 37
n00bProgrammer Avatar answered Oct 02 '22 08:10

n00bProgrammer