2

So I have the following code:

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

It seems that this only works for one UIView, which is the last one added In other words a UITapGestureRecognizer and it's view is a one to one relationship. Is this correct? How do I fix this? Do I have to create a separate UITapGestureRecog for each?

Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
xonegirlz
  • 8,889
  • 19
  • 69
  • 127

4 Answers4

5

Yes, there can be only one UITapRecogniser for one UIView. You have to take different recognizers for different views although their action can be same.
Also see this link.

Community
  • 1
  • 1
Nitish
  • 13,845
  • 28
  • 135
  • 263
0

try this,

UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];

[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];

showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];
Atulkumar V. Jain
  • 5,102
  • 9
  • 44
  • 61
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • you are NOT adding the SAME `UITapGestureRecognizer` to different views. You are always creating new ones for each UIView: `initWithTarget` initialises a new instance of `UITapGestureRecognizer`. – Fabio Jun 17 '14 at 13:35
0

I think that you just have to add the gesture recognizer to the view that contains your storyImageView, storyTitleLabel, etc. as its subviews.

Nathan Jones
  • 108
  • 1
  • 7
-1

You can add Same UITapGestureRecognizer to multiple View using this code.

Steps Are:

  1. First we create three view with tag
  2. Then we create NSMutableArray and add this view to array
  3. After that add UITapGestureRecognizer to view
  4. In UITapGestureRecognizer method we check the tag of view to differentiate which view tapped.

Here is the code for the steps:

-(Void)viewDidLoad {
    [super viewDidLoad];

    //First create three View
    UIView  *view1 = [[UIView alloc] initWithFrame: CGRectMake (5 , 171, 152, 152)];    
    view1.tag = 1;    //add tag to view
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view1];

    UIView  *  view2 = [[UIView alloc] initWithFrame: CGRectMake ( 163, 171,  152, 152)];   
    view2.tag = 2;   //add tag to view
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view2];

    UIView * view3 = [[UIView alloc] initWithFrame: CGRectMake ( 5, 330,  152, 152)];    
    view2.tag = 3;    //add tag to view
    view2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview: view2];

    //Now create mutable array to hold our view
    NSMutableArray * ary=[[NSMutableArray alloc] init];
    [ary addObject:view1];
    [ary addObject:view2];
    [ary addObject:view3];


    //now we add tap gesture to view
    for (UIView *view in ary) {
        UITapGestureRecognizer * answerDoubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(answerDoubleTapped:)];
        answerDoubleTapGesture.numberOfTapsRequired = 2;
        [answer4View addGestureRecognizer:answerDoubleTapGesture];
    }
   }

-(void)answerDoubleTapped:(UITapGestureRecognizer *)recognizer {
    //Check which view is tapped
    switch (recognizer.view.tag) {
        case 1:
        {
            NSLog(@"First View Tapped");
            break;
        }case 2:
        {
            NSLog(@"Second View Tapped");
            break;
        }case 3:
        {
            NSLog(@"Third View Tapped");
            break;
        }case 4:
        {
            NSLog(@"Forth View Tapped");
            break;
        }default:
        {
            break;
        }
    }
}
Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
  • 1
    you are NOT adding the SAME `UITapGestureRecognizer` to different views. You are always creating new ones for each UIView: `UITapGestureRecognizer * answerDoubleTapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(answerDoubleTapped:)];` `initWithTarget` initialises a new instance of `UITapGestureRecognizer`. – Fabio Jun 17 '14 at 13:32