通过 UITapGestureRecognizer 识别不同的视图标签

recognize the different view tag via UITapGestureRecognizer

我使用下面的代码在 3 个视图上添加触摸 TapGestureRecognizer。

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

但如果我点击 view1、view2、view3,下面的代码将输出 1、2、3

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

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

    NSLog(@"%@",s);

}

总是 returns 3

欢迎您的评论

我认为实现这个的方法是

[view1 addGestureRecognizer: anUITapGestureRecognizer];
[view2 addGestureRecognizer: anUITapGestureRecognizer];
[view3 addGestureRecognizer: anUITapGestureRecognizer];

you can't do like this。您需要为每个 UIView's.

创建不同的 UITapGestureRecognizer 实例

UITapGestureRecognizer 将与单个视图一起使用。您必须为每个视图

使用不同的 UITapGestureRecognizer 实例

像这样

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

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

}

希望对您有所帮助

UIGestureRecognizer 的一个实例仅适用于一个视图。 UIGestureRecognizer 有一个视图 属性 -

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.

因此您需要为每个 view 创建单独的 UIGestureRecognizer 实例(在您的情况下为 UITapGestureRecognizer),然后将它们添加到相应的 view。喜欢

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

现在它会打印对应的view's tag (1 for viewA, 2 for viewB & 3 viewC)

:

此外,您进行了相反的操作,而不是在 view 上添加 gesture 实例,而是在 gesture 上添加 view

[viewAGestureRecognizer addGestureRecognizer: viewA];

你需要按照

[viewA addGestureRecognizer: viewAGestureRecognizer];

和上面的回答一样。

UITapGestureRecognizer 只能添加到一个视图。 如果您希望在点击 view1、view2、view3 时输出为 1、2、3,则必须使用相同的选择器实例化 3 UITapGestureRecognizer,如下所示:

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