检测UITableViewCell子视图内的触摸

tpd*_*etz 8 objective-c uitableview ios

我不清楚在哪里应该将UIGestureRecognizer代码添加到UITableViewCell的相应子视图中.我已经阅读了所有可以找到的相关问题.现在我的cell和cell的子视图在cellForRowAtIndexPath中生成.我试图用cellForRowAtIndexPath添加Gesture:

UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
[mySubview addGestureRecognizer:tapGesture];
tapGesture.cancelsTouchesInView = YES;
tapGesture.delegate = self;
Run Code Online (Sandbox Code Playgroud)

但是,这没有任何检测.为了验证我的UIGesture识别器是否正常工作,我在tableView本身上使用了上面的代码,它确实按预期注册了触摸.此外,当tableView附加了上述手势时,下面的代码也按预期调用:

-(BOOL) gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    NSLog(@"shouldRevceiveTouch");
    return YES;
}

- (BOOL)gestureRecognizer:(UITapGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UITapGestureRecognizer *)otherGestureRecognizer 
{
    NSLog(@"simultaneously");
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

我试图从tableView和cellForRowAtIndexPath中删除GestureRecognizer我试图将GestureRecognizer附加到单元格本身,任何子视图,没有其他任何东西检测到触摸.(以上代码均未触发)

显然我错误地添加了GestureRecognizer.何时/何时是添加GestureRecognizer的合适位置/时间?

谢谢.

Nik*_*ook 9

我做过类似的事,但确实如此UILongPressGestureRecognizer.我认为没有太大的区别(因为所有的接触都是收到的UITableView).我在控制器viewDidLoad方法(NOT IN单元格)中添加了手势识别器.

- (void) tableViewLongPress:(UILongPressGestureRecognizer *)gestureRecognizer {
    CGPoint p = [gestureRecognizer locationInView:self.messageTableView];

    NSIndexPath *indexPath = [self.messageTableView indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"long press on table view but not on a row");
    else {
        UITableViewCell *cell = [self.messageTableView cellForRowAtIndexPath:indexPath];
        CGPoint pointInCell = [cell convertPoint:p fromView:self.messageTableView];
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以将长按更改为常规按并自行尝试


Dan*_*npe 6

我需要检测单元格内不同子视图的触摸.还处理iOS 9的UITableViewCellContentView.

首先,我在我的自定义UITableViewCell中覆盖了touchesBegan

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:touch.view];

    // Imagine I have 2 labels inside my cell
    CGPoint convertedPoint = [self.firstLabel convertPoint:point fromView:touch.view];
    if ([self.firstLabel pointInside:convertedPoint withEvent:nil]) {
        // Touched first label
        return;
    } 

    convertedPoint = [self.secondLabel convertPoint:point fromView:touch.view];
    if ([self.secondLabel pointInside:convertedPoint withEvent:nil]) {
        // Touched second label
        return;
    } 

    // no labels touched, call super which will call didSelectRowAtIndexPath
    [super touchesBegan:touches withEvent:event];
}
Run Code Online (Sandbox Code Playgroud)

要修复iOS 9中的支持,我们应该覆盖awakeFromNib,或者如果单元格不在Storyboard/xib中,则只禁用单元格用户交互.

- (void)awakeFromNib {
    // Initialization code
    self.contentView.userInteractionEnabled = NO;
} 
Run Code Online (Sandbox Code Playgroud)

当然,我们不应该忘记设置我们的标签用户交互启用.