在UITableViewCell的子视图上添加单击和双击手势

时间:2013-06-10 06:13:47

标签: iphone ios uitableview uitapgesturerecognizer

UIImageView中有UITableViewCell。我正在尝试添加单击手势以全尺寸和双击手势来查看它。这是我的代码:

    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 fail

   [aSingleTapGesture requireGestureRecognizerToFail:aDoubleTapGesture];

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

请帮助。

3 个答案:

答案 0 :(得分:1)

您可以在tableview单元格中使用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
}

答案 1 :(得分:1)

您可以将单元格的选择样式设置为

cell.selectionStyle = UITableViewCellSelectionStyleNone;

然后就是不要实现你的-tableViewDidSelectRowAtIndexPath方法

答案 2 :(得分:0)

这两行代码对我有用:

tapGesture.cancelsTouchesInView = YES;
tapGesture.delaysTouchesBegan = YES;