【问题标题】:Tap Recognition for UIImageView in the UITableViewCell在 UITableViewCell 中点击识别 UIImageView
【发布时间】:2012-07-20 08:37:45
【问题描述】:

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

问题:我该怎么做?谁能给我看代码或教程?

提前致谢!

【问题讨论】:

  • 将 UITapGestureRecognizer 添加到您的 UIImageView。

标签: objective-c ios cocoa-touch uitableview uigesturerecognizer


【解决方案1】:

这实际上比您想象的要容易。您只需要确保在 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);
}

【讨论】:

  • 在myTapMethod中,你将如何访问imageView?
  • @oyalhi UIGestureRecognizer 类有一个 view 属性,可以让你到达那里。请参阅我的更新答案。
【解决方案2】:

试试这个

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

确保你有....

imageView.userInteractionEnabled = YES;

【讨论】:

  • + 1 用于显示将识别器传递到函数中。为什么这个“tap.delegate = self;”当你不实际使用它的时候?
【解决方案3】:

你可以使用 customButton 代替 UIImageView

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-20
        • 2013-10-13
        • 2013-11-14
        • 2017-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多