2011-12-02 197 views
0

我有一个带有文本字段,按钮和表的UIWindow。 我希望能够跟踪屏幕上的所有触摸,然后将它们转发到正在触摸的元素。只将触摸事件转发给正在触摸的视图

我看了一下在Apple documentation覆盖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]; 
} 

谢谢。

回答

2

我不确定这是最好的解决方案,但我正在关注发布的内容here

基本上我有一个覆盖整个空间的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]; 
} 
+0

这工作,但倒是有从来没有传递给任何事物的视图,他们只是被转发。我想知道是否有可能“复制”事件并将它们传递给多个视图(在我的情况下,顶视图和被触摸的视图)。 –

+0

如果你只是删除'return'语句会发生什么? – Anno2001