2011-12-02 142 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