0

我正在寫一個棋盤遊戲應用程序(像國際象棋)。主視圖識別滑動手勢(UISwipeGestureRecognizer)在其全屏視圖中的任何位置開始,這使得電路板旋轉。UIControl子類 - 如何停止觸摸事件流?

現在我在主板上加了一個方形透明子視圖。這是一個亞類UIControl檢測觸摸 - 作爲板周圍的棋子移動:

[self.view addSubview:self.boardControl] 

我預期滑動手勢將在覆蓋有UIControl子類屏幕的區域被阻止。而他們不是。因此,當我快速觸摸並拖動(滑動)在我的方形boardControl上時,它首先被檢測爲棋子移動,但隨後再次檢測爲旋轉棋盤的滑動。

我該如何讓我的UIControl子類阻止在其框架中啓動的觸摸事件流向其超級視圖?


我可以阻止我的應用程序的反應,滑動手勢,由上海華盈的水平濾波觸摸,無論是由:

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer 
{ 
    CGPoint location = [gestureRecognizer locationInView:self.view]; 
    CGRect frame = self.boardControl.frame; 
    if (CGRectContainsPoint(frame, location)) 
     return NO; 
    return YES; 
} 

或:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer 
     shouldReceiveTouch:(UITouch *)touch 
{ 
    CGPoint location = [touch locationInView:self.view]; 
    CGRect frame = self.boardControl.frame; 
    if (CGRectContainsPoint(frame, location)) 
     return NO; 
    return YES; 
} 

但我想爲了早日解決這個問題:我希望boardControl不會在他們的幀觸摸事件中傳遞任何開始,在視圖層次結構中更高。

UIControl子類可以「覆蓋」它的超級視圖,並且可以「吃」所有它觸及的東西,因此超級視圖不需要訪問其框架來猜測這種觸摸是否需要過濾掉?

回答

0

所有你需要的是執行UIGestureRecognizerDelegate是提供請求的東西的方式。

我想你應該從gestureRecognizer:shouldReceiveTouch:例子

+0

開始你的意思是這樣的:
' - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer { CGPoint位置= [gestureRecognizer locationInView:self.view]。 CGRect frame = self.boardControl.frame; if([gestureRecognizer isKindOfClass:[UISwipeGestureRecognizer class]] && CGRectContainsPoint(frame,location)) return NO; }'
這有效,但是我希望我的boardControl無**通過任何觸摸/手勢,在其視圖開始。這不可能嗎? – Voyteck

+0

這段代碼不工作? – sage444

+0

此代碼有效,但是我希望我的boardControl能夠**不通過任何以其視圖開始的觸摸/手勢。這不可能嗎? *(對不起,我沒有設法將我以前的評論格式化爲代碼)* – Voyteck