ios UITapGestureRecognizer 选择器,sender 是手势,不是 ui 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6082244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:02:58  来源:igfitidea点击:

UITapGestureRecognizer selector, sender is the gesture, not the ui object

objective-ciosselectorgestures

提问by dysan819

I have a series of imageviews that I identify using their tag. I have added a single tap gesture to the images.

我有一系列使用它们的标签识别的图像视图。我在图像中添加了一个单击手势。

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];
[tableGridImage addGestureRecognizer:singleTap];
tableGridImage.userInteractionEnabled = YES;
[singleTap release];

This manages to call the selectImage selector ok, but passes the gesture as the sender. I need the imageview as the sender so I can get the tag.

这设法调用 selectImage 选择器,但将手势作为发送者传递。我需要 imageview 作为发件人,以便我可以获得标签。

Any ideas on how I can get the imageview and it's tag?

关于如何获取图像视图及其标签的任何想法?

回答by dysan819

I figured out how to get the tag, which was the most important part of the question for me. Since the gesture is the sender, I figured out the the view it is attached to is sent along with it:

我想出了如何获取标签,这对我来说是问题中最重要的部分。由于手势是发送者,我发现它所附加的视图是随它一起发送的:

[(UIGestureRecognizer *)sender view].tag

I am still curious if anyone can tell me how to send an argument through a UITapGestureRecognizer selector.

我仍然很好奇是否有人能告诉我如何通过 UITapGestureRecognizer 选择器发送参数。

回答by Samidjo

The only argument you can send through UITapGestureRecognizer selector is the UITapGestureRecognizer itself as following:

您可以通过 UITapGestureRecognizer 选择器发送的唯一参数是 UITapGestureRecognizer 本身,如下所示:

Make sure to put ":" after the selector name like you previously did :

确保像以前一样在选择器名称之后放置“ ”:

UITapGestureRecognizer *singleTap = 
[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectImage:)];

Then add a parameter to selectImage so you can retrieve the View as following:

然后向 selectImage 添加一个参数,以便您可以按如下方式检索视图:

-(void) selectImage:(UITapGestureRecognizer *)gestureRecognizer{

    //Get the View
    UIImageView *tableGridImage = (UIImageView*)gestureRecognizer.view;
}

回答by Dody Rachmat Wicaksono

From @dysan819 answer, I manage to get object without tag. In my case is UILabel.

从@dysan819 的答案中,我设法获得了没有标签的对象。在我的情况下是UILabel.

- (void)labelTap:(id)sender {
    NSLog(@"tap class: %@", [[(UIGestureRecognizer *)sender view] class]);
    if ([[(UIGestureRecognizer *)sender view] isKindOfClass:[UILabel class]]) {
        UILabel *lb = (UILabel*)[(UIGestureRecognizer *)sender view];
        NSLog(@"tap: %@", lb.text);
    }
}

回答by Norman H

If you need distinct functionality for the handler you might check out the BlocksKit project and thisfile in particular. The project is a CocoaPodsproject so you can install it easily into your toolchain.

如果您需要处理程序的独特功能,您可以查看 BlocksKit 项目,特别是这个文件。该项目是一个CocoaPods项目,因此您可以轻松地将其安装到您的工具链中。

An example from the first referenced code file:

第一个引用的代码文件中的示例:

UITapGestureRecognizer *singleTap = [UITapGestureRecognizer recognizerWithHandler:^(id sender) {
     NSLog(@"Single tap.");
 } delay:0.18];
 [self addGestureRecognizer:singleTap];

This could effectively allow you to setup a gesture recognizer easily for each image.

这可以有效地让您轻松地为每个图像设置手势识别器。