子視圖超出父視圖的部分無法響應點擊事件的處理辦法

背景

如上圖紅色區域1是父UIView,暫叫superView(就是下面代碼中的self);黑色圓形2是子UIView,暫叫childView(就是下面代碼中的self.roundImageView)。

給childView添加點擊事件,當點擊childView發現childView在superView中的區域可以響應點擊,而childView在superView外的部分無法響應點擊。

根據響應者鏈知識可以知道原因是:當點擊childView在superView外的部分,點擊事件并沒有傳遞到child,以至于無法響應點擊。

解決辦法如下:

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

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

if (!view) {? // 子類view超出父類view的部分無法響應點擊情況

CGPoint touchPointToImageView = [self.roundImageView convertPoint:point fromView:self];

if ([self.roundImageView pointInside:touchPointToImageView withEvent:event]) {

view = self.roundImageView;

}}

return view;

}

手動完成事件傳遞。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容