7

Has anyone managed to get a UIGestureRecognizer to work on a UIView that is a subview of a UIScrollView? My callbacks never seems to get called.

As a simple example, I want to have a paging scrollview and on the third page listen for a tap with a UITapGestureRecognizer. However I can not get it to work.

Here's how I would do it:

self.scrollView = [[[UIScrollView alloc] initWithFrame:self.view.frame] autorelease];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(self.section1ScrollView.frame.size.width * 3, self.scrollView.frame.size.height); //3 pages

UIImageView *p0 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page0.png"]] autorelease];
[self.scrollView insertSubview:p0 atIndex:self.scrollView.subviews.count];

UIImageView *p1 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page1.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p1 atIndex:self.scrollView.subviews.count];

UIImageView *p2 = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page2.png"]] autorelease];
//code to move it to the next page
[self.scrollView insertSubview:p2 atIndex:self.scrollView.subviews.count];

UITapGestureRecognizer *p2TapRegocnizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(p2Tapped:)] autorelease];
//p2TapRegocnizer.delegate = self;
[p2 addGestureRecognizer:p2TapRegocnizer];
Dimitris
  • 13,480
  • 17
  • 74
  • 94
  • I'm trying the same scenario. But instead of just not being called, my application is crashing. Did you manage to get this working? – tuler Aug 05 '10 at 02:20
  • No, I believe I abandoned those tests. If I get back to them I'll let you know. – Dimitris Aug 05 '10 at 12:07
  • I had the same thing, and my method is getting called, but I have added the same instance of gestureRecognizer in all of my image view i.e in p0, p1 and p2. So now i don't know when p2Tapped get called it is for which index – Yogesh Mar 21 '11 at 20:34
  • 1
    I'm having similar issues where my UIGestureRecognizers only fire for the subviews that are in the initial scrollview contentOffset where offset is 0,0 but once I scroll outside of that initial bit they don't fire (and touches themselves will not resolve to the deepest view when doing hitTest:withEvent:) – chadbag Sep 01 '12 at 00:04

2 Answers2

19

UIImageView has in default userInteractionEnabled set to NO. I would try to change it to YES.

Split
  • 4,319
  • 4
  • 19
  • 10
0
[webView setUserInteractionEnabled:YES]
user905582
  • 524
  • 1
  • 6
  • 16