子视图超出父视图的部分无法响应点击事件的处理办法

背景

子视图超出父视图的部分无法响应点击事件的处理办法_第1张图片

如上图红色区域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;

}

手动完成事件传递。

你可能感兴趣的:(子视图超出父视图的部分无法响应点击事件的处理办法)