iPhone:UITableViewController中的触摸点捕获

3
我想在UITableViewController中捕获触摸点的x位置。网络上最简单的解决方案是使用UITapGestureRecognizer:enter link description here。但在这种情况下,didSelectRowAtIndexPath被停止了。如何同时使用这两个事件,或者如何在singleTapGestureCaptured中获取(NSIndexPath *)indexPath参数?祝好。[编辑] 我无法回答我的问题。 解决方案是:NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint]
3个回答

2

我怀疑楼主可能已经不再等待答案了,但为了日后的搜索者着想:

您可以在单元格内获取触摸事件,执行操作,然后将其丢弃或传递给上层:

@interface MyCell : UITableViewCell
// ...
@end

@implementation MyCell
// ...

- (void) touchesBegan: (NSSet*) touches withEvent: (UIEvent*) event
{
    UITouch* touch = [[event allTouches] anyObject];
    CGPoint someLocation = [touch locationInView: someView];
    CGPoint otherLocation = [touch locationInView: otherView];
    if ([someView pointInside: someLocation: withEvent: event])
    {
        // The touch was inside someView. Do some stuff, 
        // but don't invoke tableView:didSelectRowAtIndexPath: on the delegate.
    }
    else if ([otherView pointInside: otherLocation: withEvent: event])
    {
        // The touch was inside otherView. Do other stuff.

        // Send the touch on for processing, and tableView:didSelectRowAtIndexPath: handling.
        [super touchesBegan: touches withEvent: event];
    }
    else
    {
        // Send the touch on for processing, and tableView:didSelectRowAtIndexPath: handling.
        [super touchesBegan: touches withEvent: event];
    }
}
@end

0

如果不搞乱表格视图处理触摸事件的方式,就不能添加手势识别器。

您没有明确说明要实现什么,因此无法推荐替代方案。任何与捕捉触摸事件有关的内容都会很复杂:响应者链是复杂的。

直接的方法似乎是在子类中重载didSelectRowAtIndexPath,在调用super之前执行所需操作...


0

楼主发布了他的答案的要点,并且它奏效了。以下是细节部分。在我的情况下,我只需要知道触摸是否在单元格的左半部分还是右半部分。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = ...;

    // Do this once for each cell when setting up the new cells...
    UITapGestureRecognizer *cellTapGestureRecognizer = [[UITapGestureRecognizer alloc]
                                                        initWithTarget:self
                                                        action:@selector(cellTapGesture:)];
    [cell.contentView addGestureRecognizer:cellTapGestureRecognizer];

    // ...
    return cell;
}

处理触摸事件或将其传递给didSelectRowAtIndexPath:

- (void)cellTapGesture:(UITapGestureRecognizer *)sender
{
    CGPoint touchPoint = [sender locationInView:self.tableView];
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
    rightBOOL = ( touchPoint.x > self.tableView.contentSize.width/2 ); // iVar

    // The UITapGestureRecognizer prevents didSelectRowAtIndexPath, so procees
    // the touch here.  Or, since only one row can be selected at a time,
    // call the old code in didSelectRowAtIndexPath and let it access
    // rightBOOL as an iVar (or pass it some other way).  Anyway, x location is known.
    [self tableView:self.tableView didSelectRowAtIndexPath:indexPath];
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接