【问题标题】:iPhone: touch point capture in UITableViewControlleriPhone:UITableViewController 中的触摸点捕获
【发布时间】:2012-01-21 17:38:38
【问题描述】:
我想在 UITableViewController 中捕捉 touch 点的 x 位置。
网上描述的最简单的解决方案是UITapGestureRecognizer:enter link description here
但在这种情况下,didSelectRowAtIndexPath 会停止。
如何使用这两个事件,或者如何在 singleTapGestureCaptured 中获取 (NSIndexPath *)indexPath 参数?
问候
[编辑]
我无法回答我的问题。
解决办法是:
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:touchPoint]
【问题讨论】:
标签:
iphone
events
location
uitableview
【解决方案1】:
我怀疑 OP 仍在等待答案,但为了未来的搜索者的利益:
您可以在单元格内获取触摸事件,执行操作,然后丢弃它,或者将其向上传递:
@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
【解决方案2】:
如果不打乱表格视图对触摸事件的处理,就无法添加手势识别器。
您没有准确说出您想要达到的目标,因此无法推荐替代方案。任何与捕获触摸事件相关的事情都会变得复杂:响应者链很复杂。
直接的方法似乎是在子类中重载 didSelectRowAtIndexPath 并在调用 super 之前做任何你想做的事情......
【解决方案3】:
OP 发布了他的答案的精髓,并且奏效了。这是详细信息。在我的例子中,我只需要知道触摸是在单元格的左半边还是右半边。
- (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];
}