在UITableViewCell中为UIImageView单击识别 [英] Tap Recognition for UIImageView in the UITableViewCell

查看:207
本文介绍了在UITableViewCell中为UIImageView单击识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



问题:我该怎么办?

解决方案



/ div>

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

   - (instancetype)initWithCoder:(NSCoder *)aDecoder 
{
if = [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);
}


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

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

Thanks in advance!

解决方案

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:

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

这篇关于在UITableViewCell中为UIImageView单击识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆