只将触摸事件转发给正在触摸的视图

我有一个文本字段,一个button和一个表的UIWindow。 我希望能够跟踪屏幕上的所有触摸,然后将它们转发到正在触摸的元素。

我已经阅读了苹果文档覆盖sendEvent ,但我仍然不明白:

  1. 如何使用hitTest来检索被触摸的元素
  2. 如何前进触摸

这是我迄今为止。

 - (void) sendEvent:(UIEvent *)event { for (UITouch *touch in [event allTouches]) { /* Get coordinates of touch */ CGPoint point = [touch locationInView:self]; /* Get subview being touched */ /* something like this??? UIView *receiver = [self hitTest:point withEvent:event]; */ /* Forward touch event to right view */ /* how??? */ } [super sendEvent:(UIEvent *)event]; } 

谢谢。

我不确定这是最好的解决scheme,但我正在按照这里发布的内容。

基本上我有一个覆盖整个空间的UIView的子类。 这样的类包含了所有可以触及的元素的引用。 (我希望有一种方法可以避免)

这是标题中的代码

  @interface SubclassUIView : UIView { UITextField *text; UITableView *table; UIButton *button; UIToolbar *toolbar; } 

这是实施:

 - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGPoint tableHit = [table convertPoint:point fromView:self]; CGPoint buttonHit = [button convertPoint:point fromView:self]; CGPoint toolbarHit = [toolbar convertPoint:point fromView:self]; CGPoint messageHit = [text convertPoint:point fromView:self]; if ([table pointInside:tViewHit withEvent:event]) return table; else if ([button pointInside:buttonHit withEvent:event]) return button; else if ([toolbar pointInside:toolbarHit withEvent:event]) return toolbar; else if ([text pointInside:messageHit withEvent:event]) return text; return [super hitTest:point withEvent:event]; } 
Interesting Posts