多个视图处理触摸事件

发布于 2024-12-19 12:19:47 字数 276 浏览 3 评论 0原文

我在 UITableView 之上有一个 UIView 的子类。我正在使用 UITableView 来显示一些数据,同时我想叠加一个跟随手指的动画(例如,留下痕迹)。

如果我做对了,我需要 UIView 子类和 UITableView 来处理触摸事件。我怎样才能做到这一点? 是否可以在 UIView 子类上触发 touchesMoved ,然后在 UITableView 上触发?

非常感谢您的帮助。

I have a subclass of UIView on top of a UITableView. I am using the UITableView to display some data and, at the same time, I would like to overlay an animation that follows the finger (for instance, leaving a trail).

If I get it right, I need the touch events to be handled both by the UIView subclass and the UITableView. How can I do that?
Is it possible to have, ie, touchesMoved being triggered on the UIView subclass and then on UITableView?

Thank you so much for any help.

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

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

发布评论

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

评论(3

随波逐流 2024-12-26 12:19:47

我解决这个问题的方法不是那么干净,但是有效。请让我知道是否有更好的方法来做到这一点。

我已经重写了自定义 UIViewhitTest ,以便它将触摸定向到下面的 UITableView 。然后在 UITableView 中,我通过 touchesBegantouchesMoved 等处理手势。在那里我还调用 touchesBeganUIView 上。

通过这种方式,触摸由两个视图处理。
我不采取相反方式的原因(让 UIViewtouchesBegan 调用 UITableViewtouchesBegan )是 UITableView 上的手势识别器无法工作。

UIView 子类的 hitTest

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // tview is the UITableView subclass instance
    CGPoint tViewHit = [tView convertPoint:point fromView:self];        
    if ([tView pointInside:tViewHit withEvent:event]) return tView;

    return [super hitTest:point withEvent:event];
}

UITableView 子类的 touchesBegan

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:touch.view];
    // ....

    // view is the UIView's subclass instance
    [view touchesBegan:touches withEvent:event];
}

The way I have solved this problem is in a way that is not that clean, but it works. Please let me know if there's a better way to do this.

I have overridden hitTest for my custom UIView so that it directs touches to the UITableView underneath. Then in the UITableView I am handling the gestures through touchesBegan, touchesMoved, etc. There I am also calling touchesBegan on the UIView.

In this way touches are handled by two views.
The reason why I am not doing the other way around (having UIView's touchesBegan calling UITableView's touchesBegan) is that gestures recognizers on the UITableView would not work.

UIView subclass' hitTest

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    // tview is the UITableView subclass instance
    CGPoint tViewHit = [tView convertPoint:point fromView:self];        
    if ([tView pointInside:tViewHit withEvent:event]) return tView;

    return [super hitTest:point withEvent:event];
}

UITableView subclass's touchesBegan

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:touch.view];
    // ....

    // view is the UIView's subclass instance
    [view touchesBegan:touches withEvent:event];
}
悸初 2024-12-26 12:19:47

不,你不能隐式地这样做。 事件传递章节说

窗口对象使用命中测试和响应者链来查找
view 接收触摸事件。在命中测试中,窗口调用
hitTest:withEvent: 在视图层次结构的最顶层视图上;这
方法通过在每个事件上递归调用 pointInside:withEvent: 来进行
返回 YES 的视图层次结构中的视图,继续向下
层次结构,直到找到触摸范围内的子视图
发生了。该视图成为命中测试视图。

因此,当窗口发现触摸视图时,它返回 YES。当前只有一个视图可以处理触摸。

但是如果你需要处理 UITableView 的事件,那么就为 UIView 处理它!您可以使用 –convertPoint, -convertRect 函数,将子视图添加到 UITableView 并根据坐标移动它,还有很多其他的事情。

No, you cann't do it implicity. Event Delivery chapter says

The window object uses hit-testing and the responder chain to find the
view to receive the touch event. In hit-testing, a window calls
hitTest:withEvent: on the top-most view of the view hierarchy; this
method proceeds by recursively calling pointInside:withEvent: on each
view in the view hierarchy that returns YES, proceeding down the
hierarchy until it finds the subview within whose bounds the touch
took place. That view becomes the hit-test view.

So, when window finds touched view it returns YES. Only one view can handle touches at the current moment.

But if you need to handle event for UITableView then handle it for UIView! You can convert touched point to required coordinates with – convertPoint, – convertRect functions, add subview to UITableView and move it depends on coordinate, and a lot of another things.

风吹雪碎 2024-12-26 12:19:47

UITableView 将未处理的触摸事件中继到 UIView。 (谷歌“响应者链”)
UITableView 文档

因此,您可以仅处理 UIView 中的触摸事件。所以。在你的 UIView 中

  1. touchesstart - 执行初始化操作

  2. touchesmove - 在 UIView 上绘制尾部(使用计时器/延迟响应来禁用点,使其看起来像一条轨迹)

  3. touchesend - 执行剩余的操作

希望这会有所帮助。

UITableView relays unhandled touch events to UIView. (Google "responder chain")
UITableView Documentation

So, you can handle your touch events in UIView only. So. In your UIView

  1. touchesstart - do initialization stuff

  2. touchesmove - draw tail on UIView (Use timers/delayedresponse to desable points so that it would look like a trail)

  3. touchesend - do remaining stuff

Hope this helps.

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