ios 在 UITableViewCell 中点击识别 UIImageView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11575779/
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 19:20:36  来源:igfitidea点击:

Tap Recognition for UIImageView in the UITableViewCell

objective-cioscocoa-touchuitableviewuigesturerecognizer

提问by Anatoliy Gatt

Currently I'm facing a problem, I would like to perform action when the UIImageView on my UITableViewCell had been tapped.

目前我面临一个问题,我想在我的 UITableViewCell 上的 UIImageView 被点击时执行操作。

Question:How could I do it? Could any one show me the code, or any tutorial?

问题:我该怎么做?任何人都可以向我展示代码或任何教程吗?

Thanks in advance!

提前致谢!

回答by Mick MacCallum

This is actually easier than you would think. You just need to make sure that you enable user interaction on the imageView, and you can add a tap gesture to it. This should be done when the cell is instantiated to avoid having multiple tap gestures added to the same image view. For example:

这实际上比您想象的要容易。您只需要确保在 imageView 上启用用户交互,并且可以向其添加点击手势。这应该在单元格被实例化时完成,以避免将多个点击手势添加到同一个图像视图中。例如:

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if (self = [super initWithCoder:aDecoder]) {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTapMethod:)];

        [self.imageView addGestureRecognizer:tap];
        [self.imageView setUserInteractionEnabled:YES];
    }

    return self;
}

- (void)myTapMethod:(UITapGestureRecognizer *)tapGesture
{
    UIImageView *imageView = (UIImageView *)tapGesture.view;
    NSLog(@"%@", imageView);
}

回答by WaaleedKhan

Try this

尝试这个

//within cellForRowAtIndexPath (where customer table cell with imageview is created and reused)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleImageTap:)];
tap.cancelsTouchesInView = YES;
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];

// handle method
- (void) handleImageTap:(UIGestureRecognizer *)gestureRecognizer 
{
    RKLogDebug(@"imaged tab");
}

make sure u have....

确保你有....

imageView.userInteractionEnabled = YES;

回答by iArezki

you can use a customButton instead UIImageView

您可以使用 customButton 代替 UIImageView

回答by Bradley

A tap gesture recogniser can be very memory hungry and can cause other things on the page to break. I would personally reconmend you create a custom table cell, insert a button into the custom cell frame and in the tableview code set the self.customtablecell.background.image to the image you want. This way you can assign the button an IBaction to make it push to whatever view you want.

点击手势识别器可能会占用大量内存,并可能导致页面上的其他内容中断。我个人建议您创建一个自定义表格单元格,在自定义单元格框架中插入一个按钮,并在 tableview 代码中将 self.customtablecell.background.image 设置为您想要的图像。通过这种方式,您可以为按钮分配一个 IBaction 以使其推送到您想要的任何视图。

回答by dimo hamdy

Use ALActionBlocksto action in block

使用ALActionBlocks在块中操作

__weak ALViewController *wSelf = self;
imageView.userInteractionEnabled = YES;
UITapGestureRecognizer *gr = [[UITapGestureRecognizer alloc] initWithBlock:^(UITapGestureRecognizer *weakGR) {
    NSLog(@"pan %@", NSStringFromCGPoint([weakGR locationInView:wSelf.view]));
}];
[self.imageView addGestureRecognizer:gr];