0

I use the code below to add touch TapGestureRecognizer on 3 views.

UITapGestureRecognizer *anUITapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doSomeThing:)];

view1.tag=1;
[view1 addGestureRecognizer:anUITapGestureRecognizer];

view2.tag=2;
[view2 addGestureRecognizer:anUITapGestureRecognizer];

//...

view3.tag=3;
[view3 addGestureRecognizer:anUITapGestureRecognizer];

same as

enter image description here

but if I tap view1, view2, view3, the code below will output 1, 2, 3

- (void)doSomething:(UITapGestureRecognizer *)tap
{

    NSString *s= [NSString stringWithFormat:@"%ld", (long)tap.view.tag];

    NSLog(@"%@",s);

}

s always returns 3

your comment welcome

yf526
  • 113
  • 7
arachide
  • 8,006
  • 18
  • 71
  • 134

5 Answers5

0

you can't do like this .You need to make different UITapGestureRecognizer instances for each UIView's.

Kumar KL
  • 15,315
  • 9
  • 38
  • 60
0

UITapGestureRecognizer is to be be used with a single view. You would have to use use different instance of UITapGestureRecognizer for each view

Something like this

for (UIView *mView in myViews) {  //Where myViews is the array that contains all views you want to have gesture recognizer on.

        UITapGestureRecognizer *tapPress = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapPress:)];
        [mView addGestureRecognizer:tapPress];
}

And

- (void)handleTapPress:(UITapGestureRecognizer*)gesture {
if ( gesture.state == UIGestureRecognizerStateEnded ) {
    UIView *selectedView =(UIView *)[gesture view];
    switch(selectedView.tag)  //there goes your conditional logic
....

}

Hope this helps

Sarim Sidd
  • 2,166
  • 2
  • 22
  • 31
0

An instance of UIGestureRecognizer will work only with one view. UIGestureRecognizer has a single view property -

view

The view the gesture recognizer is attached to. (read-only)

@property(nonatomic, readonly) UIView *view

Discussion You attach (or add) a gesture recognizer to a UIView object using the addGestureRecognizer: method.

So your need to create separate instance of UIGestureRecognizer (in your case UITapGestureRecognizer) for each view and than add them to corresponding view. Like

UIView *viewA;
UIView *viewB;
UIView *viewC;

...
// views created and customized
...
[viewA setTag:1];
[viewB setTag:2];
[viewC setTag:3];
...

// creating separate gestures and adding them to respective views
UITapGestureRecognizer *viewAGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTouched:)];
[viewA addGestureRecognizer: viewAGestureRecognizer];

UITapGestureRecognizer *viewBGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTouched:)];
[viewB addGestureRecognizer: viewBGestureRecognizer];

UITapGestureRecognizer *viewCGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(viewTouched:)];
[viewC addGestureRecognizer: viewCGestureRecognizer];


- (void) viewTouched:(UITapGestureRecognizer *)tap
{
    NSString *s= [NSString stringWithFormat:@"%ld", (long)tap.view.tag];
    NSLog(@"%@",s);
}

Now it will print the corresponding view's tag (1 for viewA, 2 for viewB & 3 for viewC)

Note :

Also you were doing the reverse rather than adding the gesture instance on view you were adding view on gesture as

[viewAGestureRecognizer addGestureRecognizer: viewA];

And you need to do it as

[viewA addGestureRecognizer: viewAGestureRecognizer];

and as above in answer.

Sanjay Mohnani
  • 5,947
  • 30
  • 46
0

The UITapGestureRecognizer can only be added to one view. If you want your output to be 1, 2, 3 when you tap view1, view2, view3, you have to instantiate 3 UITapGestureRecognizer with the same selector like this:

UITapGestureRecognizer *anUITapGestureRecognizer1 = [UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doSomething:)];
UITapGestureRecognizer *anUITapGestureRecognizer2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doSomething:)];
UITapGestureRecognizer *anUITapGestureRecognizer3 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doSomething:)];

UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view1.backgroundColor = [UIColor redColor];
view1.tag=1;
[self.view addSubview:view1];
[view1 addGestureRecognizer:anUITapGestureRecognizer1];

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0,150, 100, 100)];
view2.backgroundColor = [UIColor yellowColor];
view2.tag=2;
[self.view addSubview:view2];
[view2 addGestureRecognizer:anUITapGestureRecognizer2];

UIView *view3 = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 100, 100)];
view3.backgroundColor = [UIColor greenColor];
view3.tag=3;
[self.view addSubview:view3];
[view3 addGestureRecognizer:anUITapGestureRecognizer3]; 


- (void)doSomething:(UITapGestureRecognizer *)tap {

    NSString *s= [NSString stringWithFormat:@"%ld", (long)tap.view.tag];

    NSLog(@"%@",s); 
}    
sazz
  • 3,193
  • 3
  • 23
  • 33
-1

i think the way to implement this is

[view1 addGestureRecognizer: anUITapGestureRecognizer];
[view2 addGestureRecognizer: anUITapGestureRecognizer];
[view3 addGestureRecognizer: anUITapGestureRecognizer];
MetaSnarf
  • 5,857
  • 3
  • 25
  • 41