简体   繁体   English

iPhone:UITableViewController中的触摸点捕获

[英]iPhone: touch point capture in UITableViewController

I want to capture x position of the touch point in UITableViewController . 我想在UITableViewController中捕获触摸点的x位置 The most simply solution described in the web is UITapGestureRecognizer : enter link description here Web中描述的最简单的解决方案是UITapGestureRecognizer在此处输入链接描述

But in this case, didSelectRowAtIndexPath are stopped. 但在这种情况下, didSelectRowAtIndexPath被停止。

How tu use both of the events, or how to get (NSIndexPath *)indexPath parameter inside singleTapGestureCaptured ? tu如何使用这两个事件,或者如何在singleTapGestureCaptured中获取(NSIndexPath *)indexPath参数?

Regards 问候

[edit] I can't answer my question. [编辑]我无法回答我的问题。 The solution is: 解决方案是:

NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint] NSIndexPath * indexPath = [self.tableView indexPathForRowAtPoint:touchPoint]

I doubt the OP is still waiting for an answer, but for the benefit of future searchers: 我怀疑OP还在等待答案,但为了未来的搜索者的利益:

You can grab the touch event inside the cell, act, then either discard it, or pass it up the chain: 您可以抓住单元格内的触摸事件,执行操作,然后将其丢弃,或者将其传递到链中:

@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

The OP posted the essence of his answer, and it worked. OP公布了他答案的精髓,并且有效。 Here are the details. 这是详细信息。 In my case, I just needed to know if the touch was in the left or the right half of the cell. 在我的情况下,我只需要知道触摸是在单元格的左侧还是右侧。

- (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;
}

Process the touch or pass it on to didSelectRowAtIndexPath: 处理触摸或将其传递给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];
}

You can't add a gesture recognizer without messing up the table view's handling of touch events. 您无法在不弄乱表格视图处理触摸事件的情况下添加手势识别器。

You don't say exactly what you're trying to achieve so it's not possible to recommend an alternative. 你没有准确地说出你想要达到的目的,因此无法推荐替代方案。 Anything related to capturing touch events is going to be complicated: the responder chain is complicated. 与捕获触摸事件相关的任何事情都将变得复杂:响应者链很复杂。

The straightforward way would seem to be overload didSelectRowAtIndexPath in a child class and do whatever you want before calling super ... 直接的方式似乎是在子类中重载didSelectRowAtIndexPath并在调用super之前做任何你想做的事情......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM