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

2021-05-24  本文已影响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;
}
上一篇 下一篇

猜你喜欢

热点阅读