0

My scenario is that there will be many sub views in the main view and tapping on each sub view should generate different result.

My approach is for each sub view to implement its own tap recognizer, instead of for the main view to have one single tap recognizer and calculate the which sub view's area the user has tapped in. Is this a correct and viable approach?

I have tried this approach but it doesn't seem to work. The tap method never gets called. I read lots of articles on stackoverflow but they didn't seem to help.

For example, although the sub view is not an image view I still manually set its userInteractionEnabled property to YES, as some posts suggested. But that didn't help.

Below is the main code of the sub view:

- (void) handleOneTap:(UITapGestureRecognizer*)paramSender{
// *** Never gets called
NSUInteger touchCounter = 0; for (touchCounter = 0;
                                  touchCounter < paramSender.numberOfTouchesRequired;
                                  touchCounter++){
    CGPoint touchPoint = [paramSender locationOfTouch:touchCounter
                                               inView:paramSender.view];
    NSLog(@"Touch #%lu: %@",
          (unsigned long)touchCounter+1, NSStringFromCGPoint(touchPoint));
  }
}

- (void)viewDidLoad {
[super viewDidLoad];

_container = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 280, 300)];
[self.view addSubview:_container];

_container.backgroundColor = [UIColor redColor];
_container.opaque = YES;

// setup tap recognizer
self.tapGestureRecognizer = [[UITapGestureRecognizer alloc]
                             initWithTarget:self
                             action:@selector(handleOneTap:)];
self.tapGestureRecognizer.numberOfTouchesRequired = 1;
self.tapGestureRecognizer.numberOfTapsRequired = 1;
self.view.userInteractionEnabled = YES;
self.container.userInteractionEnabled = YES;
[self.view addGestureRecognizer:self.tapGestureRecognizer];
}

Any help will be appreciated.

Wain
  • 118,658
  • 15
  • 128
  • 151
Jack X.
  • 85
  • 2
  • 10
  • I've added a very simplified repro sample at https://www.dropbox.com/s/9qk892k7hbil2ub/Detecting%20Tap%20Gestures.zip?dl=0. If anyone could take a look it would be much appreciated. – Jack X. Jun 20 '16 at 07:27

2 Answers2

0

If you want to add UIGestureRecognizer to your subview, you should do it like that:

_container = [[UIView alloc] initWithFrame:CGRectMake(20, 50, 280, 300)]; 
[self.view addSubview:_container];

_container.backgroundColor = [UIColor redColor]; 
_container.opaque = YES;
_container.userInteractionEnabled = YES; 

self.tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleOneTap:)]; 
self.tapGestureRecognizer.numberOfTouchesRequired = 1; 
self.tapGestureRecognizer.numberOfTapsRequired = 1; 
self.view.userInteractionEnabled = YES; 


[_container addGestureRecognizer:self.tapGestureRecognizer];
Konstantin
  • 861
  • 4
  • 12
  • Thanks. I moved the _container.userInteractionEnabled = YES; statement up and changed self.view to _container which calls addGestureRecognizer but it still didn't work. – Jack X. Jun 20 '16 at 06:50
  • I've made a simplified repro at https://www.dropbox.com/s/9qk892k7hbil2ub/Detecting%20Tap%20Gestures.zip?dl=0. Could you take a look at it? Thanks very much. – Jack X. Jun 20 '16 at 07:29
  • I'll look at it in a few minutes. – Konstantin Jun 20 '16 at 08:12
0

It looks like that problem is not in UITapGestureRecognizer, but in case how you add Subview from ViewController.

Right way to add subviewcontoller is:

SSubViewController *pvc = [SSubViewController controllerWithSubViewID:0];
[self.view addSubview:pvc.view];
[self addChildViewController:pvc];

Do it in your ViewController and it should solve your problem

Konstantin
  • 861
  • 4
  • 12
  • Thank you man! That helped fix the problem in the repro. I didn't get back earlier because I encountered another issue when trying to fix the whole tapping issue in my real project. I have asked the other issue in another post at http://stackoverflow.com/questions/37922429/uitapgesturerecognizer-only-triggered-for-starting-area-of-uiscrollview. If you have time it would be appreciated if you could take a look as well! – Jack X. Jun 20 '16 at 12:25
  • If it helps, please mark it as correct answer. That could help others. – Konstantin Jun 20 '16 at 13:49