背景:
如上圖紅色區域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;
}
手動完成事件傳遞。