手势识别器和 TableView

发布于 2024-10-08 00:02:04 字数 115 浏览 0 评论 0原文

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

I have a UIView which covers all of a UITableView.
The UIView is using gesture recognizers for control of what the table displays.
I still need the vertical UITableView scrolling and row taps.
How do I pass these on to the table from the gesture recognizers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿! 2024-10-15 00:02:04

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

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

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

Assign your gesture to the table view and the table will take care of it:

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

Then in your gesture action method, act based on the direction:

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

The table will only pass you the gestures you have asked for after handling them internally.

暮倦 2024-10-15 00:02:04

如果您需要知道单元格的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问题中得到解答。

If you need to know your cell's indexPath:

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

This was previously answered in UIGestureRecognizer and UITableViewCell issue.

韵柒 2024-10-15 00:02:04

我尝试了罗伯·邦纳的建议,效果很好。谢谢。

但是,就我而言,方向识别存在问题。 (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:我的英语不太好,所以如果有任何句子错误,很高兴纠正:)

I tried Rob Bonner's suggestion and it works great. thank you.

But, in my case, there's a problem with direction recognition. (recognizer.direction always refer 3) I'm using IOS5 SDK and Xcode 4.

It seems caused by "[gesture setDirection:(left | right)]" I think. (because predefined (dir left | dir right) calculation result is 3)

So, if someone has problem like me and want to recognize swipe left and right seprately, then assign two recognizer to table view with different directions.

Like this:

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];

and gesture action below:

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

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

I coded with ARC feature then if you are not using ARC, add release codes.

PS: My english is not so good, so if there's any sentential error, correction will be very pleased :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文