2012-01-22 192 views
0

我有一個啓用分頁的UIScrollview。這個滾動視圖內有3個視圖(頁面)。在滾動視圖的父視圖中有一個輕擊手勢,在頂部顯示並隱藏導航欄。如何將TouchUpInside事件傳遞給UIButton,而不將它傳遞給父視圖?

問題: 在我想添加按鈕的頁面之一中。但問題是,每當我點擊這些按鈕時,顯示/隱藏導航欄方法也被觸發。將觸摸僅傳遞給這些按鈕而不是滾動視圖的父視圖的最佳方式是什麼?

回答

1

NJones是在正確的軌道上,但我認爲他的答案有一些問題。

我假設你想通過觸摸任何按鈕在你的滾動視圖。在你的手勢識別器的代表中,執行如下gestureRecognizer:shouldReceiveTouch:

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)recognizer shouldReceiveTouch:(UITouch *)touch { 

    UIView *gestureView = recognizer.view; 
    // gestureView is the view that the recognizer is attached to - should be the scroll view 

    CGPoint point = [touch locationInView:gestureView]; 
    UIView *touchedView = [gestureView hitTest:point withEvent:nil]; 
    // touchedView is the deepest descendant of gestureView that contains point 

    // Block the recognizer if touchedView is a UIButton, or a descendant of a UIButton 
    while (touchedView && touchedView != gestureView) { 
     if ([touchedView isKindOfClass:[UIButton class]]) 
      return NO; 
     touchedView = touchedView.superview; 
    } 
    return YES; 
} 
+0

太棒了!我錯過了他如何說複數。 Tisk tisk。我的答案是從我的其他答案之一中複製出來的,這是一種排除方式。這對於倍數更好。 – NJones

+0

我現在就試試看。奇怪的是,我沒有從Stackoverflow得到關於此的通知。但這看起來確實是一個可靠的解決方案。謝謝你們回覆! – Sumit

相關問題