簡體   English   中英

處理父視圖觸摸

[英]Handling parent view touches

我有3個子視圖的UIView,我也需要處理在超級視圖和子視圖上的每個觸摸,但是子視圖會攔截觸摸。 我怎樣才能做到這一點? 謝謝。 UPD:如此簡單的方法不存在嗎? 事實證明,將事件與繼承分開比較容易。

您可以使用禁用子視圖接受觸摸

[subView setUserinteractionEnabled: NO];

這樣,它們將不會攔截觸摸事件,並且只會將它們發送到父視圖。

如果希望兩個視圖都接收事件,則可以在子視圖中捕獲事件,然后將其手動發送到父視圖。

您需要在子視圖中覆蓋觸摸事件,如下所示:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesBegan: touches withEvent:event]; 
    [super touchesBegan: touches withEvent: event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nextResponder touchesMoved: touches withEvent:event]; 
    [super touchesMoved: touches withEvent: event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    [super touchesEnded: touches withEvent: event];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM