2012-01-04 36 views
1

很簡單的問題,或許不是那麼簡單的答案,但:如何將觸摸傳遞給另一個視圖下方的按鈕?

我已經清楚地看到需要接觸的部分。 下面這是一個UIButton,我也想要接觸(因爲我不會進入的原因,它必須在下面)。在按下按鈕的情況下,我不希望清晰的視圖接收觸摸。

我怎樣才能做到這一點?

編輯:

最終解決方案:

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { 


    for (UIView * view in self.subviews) 
    { 
     if ([view isKindOfClass:[UIButton class]]) { 
      CGPoint pointInButton = [view convertPoint:point fromView:self]; 
      if ([view pointInside:pointInButton withEvent:event]) { 
       return view; 
      } 
     } 
    } 
    return [super hitTest:point withEvent:event]; 
} 
+0

你有沒有嘗試過爲你的清晰視圖設置'userInteractionEnabled'爲false? – aroth 2012-01-04 02:33:06

+0

@aroth我也需要清晰的觀點才能接觸到。對不起,應該在問題中更具體(我現在將編輯)。 – 2012-01-04 02:34:36

回答

2

給清晰視圖的UIButton的參考。覆蓋清晰視圖的pointInside:withEvent:方法。在您的覆蓋中,檢查點是否在按鈕內(通過發送pointInside:withEvent:到按鈕)。如果該點在按鈕中,則返回NO。如果點在按鈕外面,則爲return [super pointInside:point withEvent:event]

+1

在這裏額外的討論:http://stackoverflow.com/questions/1694529/allowing-interaction-with-a-uiui-under-another-uiview – aroth 2012-01-04 02:49:38

+0

謝謝!這是一個很好的答案。我有點卡住得到它的工作 - 任何機會,你會說,爲什麼我使用的代碼將無法正常工作(我已經添加到我的問題)? – 2012-01-04 03:37:18

+0

Aha ...用下面這行代碼修復:CGPoint pointInButton = [view convertPoint:point fromView:self];謝謝 :) – 2012-01-04 03:46:01

相關問題