iphone - 在 UITableViewCell 的 subview 上添加单击和双击手势

标签 iphone ios uitableview uitapgesturerecognizer

我在 UITableViewCell 中有一个 UIImageView。我正在尝试添加单击手势以全尺寸查看它,并添加双击手势以进行喜欢。这是我的代码:

    UITapGestureRecognizer *aSingleTapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTapGestureRecognizer:)] autorelease];
    aSingleTapGesture.delegate = self;
    aSingleTapGesture.numberOfTapsRequired = 1;
    [cell.imgProductImage addGestureRecognizer:aSingleTapGesture];

    UITapGestureRecognizer *aDoubleTapGesture = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapGestureRecognizer:)] autorelease];
    aDoubleTapGesture.delegate = self;
    aDoubleTapGesture.numberOfTapsRequired = 2;
    [cell.imgProductImage addGestureRecognizer:aDoubleTapGesture];

它只调用单击手势,双击手势从未被调用。比我在单击手势上使用require to failed

   [aSingleTapGesture requireGestureRecognizerToFail:aDoubleTapGesture];

好吧,使用这段代码我可以调用单击和双击手势,但问题是 tableView:didSelectRowAtIndexPath: 也被调用。如果我的手势被调用,我不想调用 didSelectRowAtIndexPath 。据我了解,它被调用是因为在调用 didSelectRowAtIndexPath 时单击手势正在等待双击手势。

请提供任何帮助。

最佳答案

您可以在表格 View 单元格中使用 UIButton 代替 UIImageView。使用以下代码获得单击和双击。

//add target for your UITableViewCell's button

[aBtnThumbObj addTarget:self action:@selector(btnThumbClickDouble:) forControlEvents:UIControlEventTouchDownRepeat];

[aBtnThumbObj addTarget:self action:@selector(btnThumbClickSingle:) forControlEvents:UIControlEventTouchUpInside];

-(void)btnThumbClickSingle:(UIButton*)sender
{
    [self performSelector:@selector(singleTapOnButtonSelector:) withObject:sender afterDelay:0.2];
}
-(void)btnThumbClickDouble:(UIButton*)sender
{   
   [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(singleTapOnButtonSelector:) object:sender];
//Your implementation for double tap
}
-(void)singleTapOnButtonSelector:(UIButton*)sender
{
//Your implementation for single tap
}

关于iphone - 在 UITableViewCell 的 subview 上添加单击和双击手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17017803/

相关文章:

ios - 放4个导航栏按钮

iphone - 如何从iphone IOS上的时间间隔解析和提取年,月,日等

objective-c - NSNumber 与 NSInteger 与 NSObject 属性的 int

swift - 如何以编程方式在容器内实现 TableView?

iphone - Phonegap iOS 应用程序屏幕在双击时向上滑动

iphone - 在 iPhone 上不推荐使用 Constant kCGColors?

ios - UIGestureRecognizer 知道调用它的对象是什么吗?

ios - 禁用特定时间段的通知

iphone - 如何阻止 UITableViewCell 中的文本进入披露指示器使用的区域?

ios - 在 xamarin 中调整单元格大小