美文网首页
UIView超出父view的部分视图的子视图响应事件

UIView超出父view的部分视图的子视图响应事件

作者: 野码道人 | 来源:发表于2021-08-18 11:41 被阅读0次
    - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
        UIView *view = [super hitTest:point withEvent:event];
        CGPoint clickPoint = CGPointZero;
        if (view == nil) {
            for (UIView *subView in self.subviews) {
                clickPoint = [subView convertPoint:point fromView:self];
                if (CGRectContainsPoint(subView.bounds, clickPoint)) {
                    view = subView;
                    break;
                }
            }
        }
        CGPoint finalClickPoint = clickPoint;
        if ([view isMemberOfClass:NSClassFromString(@"超出父view的视图类名")]) {
            for (UIView *subView in view.subviews) {
                finalClickPoint = [subView convertPoint:clickPoint fromView:view];
                if (CGRectContainsPoint(subView.bounds, finalClickPoint)) {
                    view = subView;
                    break;
                }
            }
        }
        return view;
    }
    
    • hitTest方法决定最终响应事件的视图,不会再次分发,因此这个方法里面要直接分发到需要响应事件的视图,如上每多一个层级就多一层循环

    相关文章

      网友评论

          本文标题:UIView超出父view的部分视图的子视图响应事件

          本文链接:https://www.haomeiwen.com/subject/roolmltx.html