检测 UIScrollView 内 UITableViewCell 上的滑动手势

发布于 2024-12-06 19:03:26 字数 1300 浏览 0 评论 0原文

我希望有人能够帮助我解决目前困扰我的问题!

给定以下视图层次结构

在此处输入图像描述

我希望能够检测自定义 UITableViewCell 上的滑动手势。

我已经对 UIScrollView 进行了子类化,并有一个 hitTest:withEvent: 方法来检查我是否触摸了 tableview 单元格(或其内容),在这种情况下,我设置了以下滚动视图属性:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];
    if ([result.superview isKindOfClass:[UITableViewCell class]] || [result.superview tag] == SUBVIEW_TAG)
    {
        self.canCancelContentTouches = NO;  
        self.delaysContentTouches = YES;
    } else {
        self.canCancelContentTouches = YES;
        self.delaysContentTouches = NO;
    }
    return result;
}

我还实现了:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    if (view.tag == SUBVIEW_TAG || [[view superview] isKindOfClass:[UITableViewCell class]])
        return NO;
    return YES;
}

并且返回 NO如果被触摸的视图是表视图单元格。

这些方法都被调用并按预期执行其操作,但我仍然无法阻止 UIScrollView “霸占”滑动手势。

有趣的是,如果我在上面的两种方法(带有 SUBVIEW_TAG 的方法)中包含包含 tableview 和 cell 的 UIView,它会完美地工作,所以我猜测这一定与 UITableView 继承自 UIScrollView 这一事实有关。

我的主要目标是能够在单元格上滑动以显示单元格的更多选项。该视图上任何其他位置的水平滑动将被滚动视图捕获,并按照其正常行为水平移动内容。

任何想法将不胜感激!

谢谢! 罗格

I am hoping someone will be able to help me with a problem that is doing my head in at the moment!

Given the following view hierarchy

enter image description here

I want to be able to detect swipe gestures on my custom UITableViewCell.

I have subclassed the UIScrollView and have a hitTest:withEvent: method that checks whether I am touching the tableview cell (or its content) or not, in which case I set the following scroll view properties:

- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView* result = [super hitTest:point withEvent:event];
    if ([result.superview isKindOfClass:[UITableViewCell class]] || [result.superview tag] == SUBVIEW_TAG)
    {
        self.canCancelContentTouches = NO;  
        self.delaysContentTouches = YES;
    } else {
        self.canCancelContentTouches = YES;
        self.delaysContentTouches = NO;
    }
    return result;
}

I have also implemented:

- (BOOL)touchesShouldCancelInContentView:(UIView *)view
{
    if (view.tag == SUBVIEW_TAG || [[view superview] isKindOfClass:[UITableViewCell class]])
        return NO;
    return YES;
}

And am returning NO in case the view being touched is the table view cell.

These methods are all getting called and performing their actions as expected, but I am still unable to stop the UIScrollView from "hogging" the swipe gesture.

The interesting thing is that if I include the UIView that contains the tableview and cell on both of the methods above (the one with SUBVIEW_TAG) it works perfectly so I am guessing it must be something to do with the fact that UITableView inherits from UIScrollView.

My main goal is to be able to swipe on the cell to reveal more options for the cell. A horizontal swipe anywhere else on that view would be captured by the scroll view and shift the content horizontally as per its normal behaviour.

Any ideas would be very much appreciated!

Thanks!
Rog

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

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

发布评论

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

评论(2

风吹过旳痕迹 2024-12-13 19:03:26

我在滚动视图内的组件的滑动检测方面遇到了类似的问题,并且我能够使用

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:swipeGesture]

其中scrollView是滚动视图对象,其行为类似于容器和swipeGesture是滚动视图内的组件滑动手势对象来解决它。

因此,您可以像这样为单元格对象定义滑动(对于示例中的右滑动,根据需要自定义它)

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

[cell addGestureRecognizer:rightSwipeRecognizer];

,然后执行

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipeRecognizer]

requireGestureRecognizerToFail 说:

此方法创建与另一个手势识别器的关系
这延迟了接收器的转换
UIGestureRecognizerState可能。接收方的状态
转换为取决于 otherGestureRecognizer 发生的情况:

如果 otherGestureRecognizer 转换为
UIGestureRecognizerStateFailed,接收器转换到正常状态
下一个状态。

如果 otherGestureRecognizer 转换为
UIGestureRecognizerStateRecognized 或 UIGestureRecognizerStateBegan,
接收器转换为 UIGestureRecognizerStateFailed。

当您想要一个
单击手势要求双击手势失败。

可用性 适用于 iOS 3.2 及更高版本。

希望有帮助!

I had a similar problem with a swipe detect for a component inside a scrollview and I was able to resolve it with

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:swipeGesture]

Where scrollView is the scroll view object that acts like container and swipeGesture is the component swipe gesture object inside scrollview.

So, you can define a swipe for the cell object like this (for right swipe in the example, custom it as you want)

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

[cell addGestureRecognizer:rightSwipeRecognizer];

and then do

[scrollView.panGestureRecognizer requireGestureRecognizerToFail:rightSwipeRecognizer]

The documentation of requireGestureRecognizerToFail says:

This method creates a relationship with another gesture recognizer
that delays the receiver’s transition out of
UIGestureRecognizerStatePossible. The state that the receiver
transitions to depends on what happens with otherGestureRecognizer:

If otherGestureRecognizer transitions to
UIGestureRecognizerStateFailed, the receiver transitions to its normal
next state.

if otherGestureRecognizer transitions to
UIGestureRecognizerStateRecognized or UIGestureRecognizerStateBegan,
the receiver transitions to UIGestureRecognizerStateFailed.

An example where this method might be called is when you want a
single-tap gesture require that a double-tap gesture fail.

Availability Available in iOS 3.2 and later.

Hope helps!

迷乱花海 2024-12-13 19:03:26

解决方案非常简单。您需要做的就是将 UIScrollView 添加到 UITableViewCell 中。它将防止滑动手势期间的“霸占”效应。

嵌入式 UIScrollView

The solution is pretty simple. All you need to do is add UIScrollView inside you UITableViewCell. It will prevent "hogging" effect during swipe gesture.

Embedded UIScrollView

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