2013-03-05 37 views
0

我有一個子類UIView,我將調用customView。我想啓用觸摸,以便用戶可以操作子視圖,其中包含手勢識別器和其他控件,但是視圖本身我不想觸摸,以便在視圖下方繪製的視圖仍然可以觸摸。換句話說,customView將在應用程序中的其他視圖之上繪製,但我仍然希望以下視圖可觸摸,同時允許觸及customView的子視圖。iOS定義了一個對象的可觸摸區域,將觸摸侷限在自我的子視圖上

我試過用touchesBegan這樣,但這不起作用。有任何想法嗎?謝謝閱讀!

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch *touch = [[event allTouches] anyObject]; 

//I've tagged the views that I want to be touchable. 
    if ([touch view].tag == 1000 || [touch view].tag == 2000 || [touch view].tag == 3000) { 
     self.userInteractionEnabled = YES; 
    } else { 
     self.userInteractionEnabled = NO; 
    } 
} 
+0

所以,你要cusomView nontouchable和查看以下cusomview必須是可觸摸的權利? – Dilip 2013-03-05 11:36:13

+0

@Dilip我想從視圖的區域「屏蔽」/排除觸摸,除了在某些子視圖中。 – Mrwolfy 2013-03-05 11:56:51

回答

1

你需要做的是實現以下方法在customView:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event 
{ 
    //check if one of the subviews was hit, if so forward the touch event to it 
    for (UIView *view in self.subviews){ 
     if (CGRectContainsPoint(view.frame, point)) 
      return view; 
    } 

    // use this to pass the 'touch' upward in case no subviews trigger the touch 
    return [super hitTest:point withEvent:event]; 
} 
+0

@Mrwolfy你有沒有試過我的答案,我很想知道它是否適用於你,因爲我有類似的問題,這種方法適用於我,但它不完全相同,你的情況.. – 2013-03-07 12:17:32

0

然後上添加這些子視圖您customview.other副您customview不應該讓他們touche.and把自定義視圖的子視圖下方,從而讓利面積那麼你的選擇子視圖會動心了。