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

- (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的部分视图的子视图响应事件)