0
votes

I have a scenario like shown below Scenario

  1. Right now, I am showing only 1 view with label 8. But I am planning to add 3 more such views to the HolderView.

  2. The SmallerView/s are created from other Nib files.

I did this code for adding Tap Recognizer for ViewController's view

UITapGestureRecognizer *tapRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self                                                                         action:@selector(tapRecognized2:)];
[tapRecognizer setDelegate:self];
[self.view addGestureRecognizer:tapRecognizer];

Code for adding Tap Recognizer to the smaller views I added smaller views to the HolderView. And assigned Tag IDs to them. After that,

for (SmallerView *view in HolderView.subviews) {
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRecognized:)];
    [recognizer setDelegate:self];
NSLog(@"Added gesture to the view with tag: %ld",view.tag);

    [view addGestureRecognizer:recognizer];
}

3. - (void)tapRecognized:(UITapGestureRecognizer *)paramSender { NSLog(@"tapped on %ld", paramSender.view.tag); }

- (void)tapRecognized2:(UITapGestureRecognizer *)paramSender
{
    NSLog(@"VC view");
}
  1. I have enabled UserInteraction (both in code and Inspector) for all the views and UILabels on smaller views too.

The problem now is... The smaller view's Tap recognisers are not really working consistently. Sometimes they print the output. All at suddenly it prints the ViewController's recogniser's output. Please help

UPDATE: Below is my View diagram. Green Border: (In UIView's initWithFrame)d

self.layer.borderColor = [UIColor greenColor].CGColor;

Red Border:

    MyTile *tile = [[[NSBundle mainBundle] loadNibNamed:@"View" owner:self options:nil] objectAtIndex:0];
self.myLabel.layer.borderColor=[UIColor redColor].CGColor;

Why that Green Border is coming only of that size? Shouldn't that be full square? And, the gesture works only when I tap on the green area. Why? enter image description here

3

3 Answers

1
votes

It seems that you have a tap gesture recognizer on a view and also a tap gesture recognizer on its superview. The way gesture recognizers work is that, by default, both of them will be candidates to recognize the tap. If that is not what you want, it is up to you to prevent it.

You can:

  • set up a "you-go-first" relationship between the gesture recognizers, or

  • you can use their delegates to help decide between them, or

  • you can set up the subview so that it stops the superview's gesture recognizer from recognizing.

You have lots of options!

But if you do none of them, then both this view's gesture recognizer and its superview's gesture recognizer will be trying to recognize.

0
votes

Perhaps a better solution would just to add one tapGestureRecognizer to the parent view.

UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]init];
tap.numberOfTapsRequired = 1;
tap.numberOfTouchesRequired = 1;
[tap addTarget:self action:@selector(handleTap:)];
[holderView addGestureRecognizer:tap];

Then add the target method

- (void) handleTap:(UITapGestureRecognizer *)tap {
    UIView * holderView;

    CGPoint tapPoint = [tap locationInView:holderView];
    for (UIView * v in holderView.subviews) {
        if (CGRectContainsPoint(v.frame, tapPoint)) {
            // v is the subview that was pressed.
            // add your code here.


            break;
        }
    }
}
0
votes

I have assigned a new frame to the frame which was having wrong borders. This helped in getting the gestures right.