触摸事件由多个视图处理

我有一个UITableViewUIView的子类。 我正在使用UITableView来显示一些数据,同时,我想覆盖手指后面的animation(例如,留下踪迹)。

如果我得到它的权利,我需要触摸事件由UIView子类和UITableView 。 我怎样才能做到这一点? 是否有可能,即touchesMoved在UIView子类,然后在UITableView触发?

非常感谢你的帮助。

我已经解决了这个问题的方式是不那么干净,但它的工作原理。 请让我知道是否有更好的方法来做到这一点。

我已经重写了我的自定义UIView hitTest ,所以它指向下面的UITableView触摸。 然后在UITableView我通过touchesBegantouchesMoved等处理手势。在那里,我也调用UIView上的touchesBegan

这样触摸就是由两个视图来处理的。 我之所以不这样做(有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]; } 

不,你不能这么做。 事件传递章节说

窗口对象使用命中testing和响应者链来查找接收触摸事件的视图。 在命中testing中,窗口调用hitTest:withEvent:在视图层次结构的最顶层视图上; 此方法通过recursion调用pointInside:withEvent:在视图层次结构中的每个视图中返回YES,继续向下继续,直到find触摸发生的边界内的子视图。 这个观点成为了命中testing的观点。

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

但是,如果你需要处理UITableView的事件,然后处理它的UIView! 您可以将触摸的点转换为所需的坐标与- convertPoint , – convertRect函数,将子视图添加到UITableView并移动它取决于坐标,还有很多其他的东西。

UITableView将未处理的触摸事件传递给UIView。 (Google“响应者链”) UITableView文档

所以,你只能在UIView中处理你的触摸事件。 所以。 在你的UIView

  1. touchesstart – 做初始化的东西

  2. touchesmove – 在UIView上绘制尾巴(使用定时器/延迟响应到desa​​ble点,使它看起来像一个踪迹)

  3. touchesend – 做剩下的东西

希望这可以帮助。