美文网首页
设置子类UIView 本实例不会接受点击事件

设置子类UIView 本实例不会接受点击事件

作者: 奋斗的小蜗牛_ | 来源:发表于2021-05-24 17:28 被阅读0次

subView - >
@property (nonatomic, copy) void(^didTapBlankBlock)(void);
具体实现 重写hitTest方法。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == self) {
        view = nil;
        if (self.didTapBlankBlock) {
            self.didTapBlankBlock();
        }
    }
    return view;
}

创建类似VC

- (void)loadView {
    self.view = [[ alloc] initWithFrame:UIApplication.sharedApplication.keyWindow.bounds];
}

识别当前最顶层VC

UIViewController *rootVC = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
while (resultVC.presentedViewController) {
        resultVC = [self fetch_topViewController:resultVC.presentedViewController];
    }
    return resultVC;

+ (UIViewController *)fetch_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self fetch_topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self fetch_topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

相关文章

网友评论

      本文标题:设置子类UIView 本实例不会接受点击事件

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