34

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?

2
  • let me know if you solved your problem.
    – BhavikKama
    Oct 7, 2013 at 6:20
  • As per my code its wrking fine in landscape but not wrking in portrait Oct 7, 2013 at 7:03

2 Answers 2

115

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];
5
  • 1
    i have add 10 imageview all working fine except uiimageview in which one imageview is placed over another image view Oct 7, 2013 at 6:30
  • 1
    no it is still not working .. in portrait but in landscape its working fine Oct 7, 2013 at 6:59
  • 1
    Nice! Just clicked that on for the image view in Storyboard and it all works fine. Thanks. Feb 3, 2016 at 5:43
  • 1
    Superb!! you are awesome man, how can i forgot setUserInteractionEnabled
    – swiftBoy
    Apr 27, 2016 at 11:55
  • 1
    you can also set this using interface builder in image properties Apr 9, 2017 at 12:02
2
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
2
  • tap gesture is working fine when two imageview placed in different location. problem is when two imageview placed in same location. hidden one in first time then hidden another in second time Oct 7, 2013 at 6:32
  • So you want to toggle the visibility of imageViews on user tap, at the same location ? Oct 7, 2013 at 6:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.