触摸事件由多个视图处理 [英] Touch event handled by multiple views

查看:130
本文介绍了触摸事件由多个视图处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 UITableView 之上有一个 UIView 的子类。我使用 UITableView 来显示一些数据,同时,我想覆盖手指后面的动画(例如,留下一条线索)。 / p>

如果我做对了,我需要通过 UIView c>子类和 UITableView 。我怎样才能做到这一点?
是否有可能在UIView子类上触发 touchesMoved 然后在 UITableView 上触发?



非常感谢您的帮助。

解决方案

方式我已经解决了这个问题的方式不是那么干净,但它的工作原理。如果有更好的方法,请告诉我。



我已经覆盖 hitTest 我的自定义 UIView 以便它将触摸指向下面的 UITableView 。然后在 UITableView 我通过 touchesBegan 处理手势, touchesMoved 等。在那里我也在 UIView 上调用 touchesBegan



这样,触摸由两个视图处理。
之所以我没有做相反的事情(有 UIView touchesBegan 调用 UITableView touchesBegan )是 UITableView 上的手势识别器不工作。



UIView 子类' hitTest

   - (UIView *)hitTest:(CGPoint)指向withEvent:(UIEvent *)事件
{
/ / tview是UITableView子类实例
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 *)触及withEvent: (UIEvent *)事件
{
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];
// ....

// view是UIView的子类实例
[view touchesBegan:touches withEvent:event];
}


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.

解决方案

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

这篇关于触摸事件由多个视图处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆