亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何忽略觸摸事件并將其傳遞給另一個子視圖的UIControl對象?

如何忽略觸摸事件并將其傳遞給另一個子視圖的UIControl對象?

iOS
尚方寶劍之說 2019-12-21 12:58:19
我有一個自定義的UIViewController,其UIView占據了屏幕的一角,但是除了其中有一些按鈕和內容的部分以外,其余大部分都是透明的。由于該視圖上對象的布局,該視圖的框架可以掩蓋其下方的一些按鈕。我希望能夠忽略該視圖上的任何觸摸,如果它們沒有觸摸任何重要的東西,但是我似乎只能傳遞實際的觸摸事件(touchesEnded / nextResponder的東西)。如果我有一個UIButton或類似的不使用touchesEnded的東西,如何將touch事件傳遞給它?我不能只是手動找出要調用的按鈕選擇器,因為此自定義ViewController可以用于許多不同的視圖。我基本上需要一種方法來稱呼它:[self.nextResponder touchesEnded:touches withEvent:event];以及UIControl類型。
查看完整描述

3 回答

?
開滿天機

TA貢獻1786條經驗 獲得超13個贊

可能最好的方法是hitTest:withEvent:在要忽略觸摸的視圖中重寫。根據視圖層次結構的復雜性,有兩種簡單的方法可以實現此目的。


如果在該視圖下有對該視圖的引用,則可以忽略:


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

{

    UIView *hitView = [super hitTest:point withEvent:event];


    // If the hitView is THIS view, return the view that you want to receive the touch instead:

    if (hitView == self) {

        return otherView;

    }

    // Else return the hitView (as it could be one of this view's buttons):

    return hitView;

}

如果您沒有對該視圖的引用:


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

{

    UIView *hitView = [super hitTest:point withEvent:event];


    // If the hitView is THIS view, return nil and allow hitTest:withEvent: to

    // continue traversing the hierarchy to find the underlying view.

    if (hitView == self) {

        return nil;

    }

    // Else return the hitView (as it could be one of this view's buttons):

    return hitView;

}

我建議第一種方法最可靠(如果有可能獲得對基礎視圖的引用)。


查看完整回答
反對 回復 2019-12-21
  • 3 回答
  • 0 關注
  • 696 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號