13

我有一个涵盖所有 UITableView 的 UIView。UIView 使用手势识别器来控制表格显示的内容。我仍然需要垂直 UITableView 滚动和行点击。我如何将这些从手势识别器传递到桌子上?

4

3 回答 3

31

如果您需要知道单元格的 indexPath:

- (void)handleSwipeFrom:(UIGestureRecognizer *)recognizer {
    CGPoint swipeLocation = [recognizer locationInView:self.tableView];
    NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
    UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
}

这之前在UIGestureRecognizer 和 UITableViewCell 问题中得到了回答。

于 2012-01-21T18:17:20.827 回答
30

将您的手势分配给表格视图,表格将处理它:

UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc]
        initWithTarget:self action:@selector(handleSwipeFrom:)];
[gesture setDirection:
        (UISwipeGestureRecognizerDirectionLeft
        |UISwipeGestureRecognizerDirectionRight)];
[tableView addGestureRecognizer:gesture];
[gesture release];

然后在您的手势操作方法中,根据方向进行操作:

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        [self moveLeftColumnButtonPressed:nil];
    }
    else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        [self moveRightColumnButtonPressed:nil];
    }
}

该表只会在内部处理后将您要求的手势传递给您。

于 2010-12-16T16:59:57.763 回答
7

我尝试了 Rob Bonner 的建议,效果很好。谢谢你。

但是,就我而言,方向识别存在问题。(recognizer.direction 总是参考 3)我使用的是 IOS5 SDK 和 Xcode 4。

我认为这似乎是由“[gesture setDirection:(left | right)]”引起的。(因为预定义的(dir left | dir right)计算结果是3)

所以,如果有人像我一样有问题并且想分别识别左右滑动,那么将两个识别器分配给具有不同方向的表格视图。

像这样:

UISwipeGestureRecognizer *swipeLeftGesture = [[UISwipeGestureRecognizer alloc] 
                                             initWithTarget:self
                                             action:@selector(handleSwipeLeft:)];
[swipeLeftGesture setDirection: UISwipeGestureRecognizerDirectionLeft];

UISwipeGestureRecognizer *swipeRightGesture = [[UISwipeGestureRecognizer alloc] 
                                              initWithTarget:self 
                                              action:@selector(handleSwipeRight:)];

[swipeRightGesture setDirection: UISwipeGestureRecognizerDirectionRight];

[tableView addGestureRecognizer:swipeLeftGesture];
[tableView addGestureRecognizer:swipeRightGesture];

和下面的手势动作:

- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {
    [self moveLeftColumnButtonPressed:nil];
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
    [self moveRightColumnButtonPressed:nil];
}

我使用 ARC 功能进行编码,然后如果您不使用 ARC,请添加发布代码。

PS:我的英文不太好,如有错误,望指正:)

于 2012-03-28T07:35:52.263 回答