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

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;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容