xonegirlz
xonegirlz

Reputation: 8969

adding one gesture recognizer to a few UIView

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?

Upvotes: 2

Views: 8326

Answers (4)

Dilip Manek
Dilip Manek

Reputation: 9143

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;
        }
    }
}

Upvotes: -1

Vignesh
Vignesh

Reputation: 10251

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];

Upvotes: 0

Nathan Jones
Nathan Jones

Reputation: 108

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

Upvotes: 0

Nitish
Nitish

Reputation: 14113

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.

Upvotes: 5

Related Questions