首页 > 代码库 > 自定义弹框,点击提示框外空白区域,让弹框消失
自定义弹框,点击提示框外空白区域,让弹框消失
tip:
- self.mainView 是提示框的全屏背景。一般是透明的黑色
- self.bgImg 添加提示内容的主要view
方法一:正常情况下,各个页面都有touchesBegan:withEvent事件的触发。使用该方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; // 计算搜索框范围 CGPoint touchPoint = [touch locationInView:self.mainView]; NSLog(@"x = %f, y = %f", touchPoint.x, touchPoint.y); if (touchPoint.x > (self.bgImg.kkg_x+self.bgImg.kkg_width)&& touchPoint.x < self.bgImg.kkg_x && touchPoint.y > (self.bgImg.kkg_y+self.bgImg.kkg_height) && touchPoint.y < self.bgImg.kkg_y) { } else { [self closeAlert]; }}
方法二:当某个触发事件中断。 有个简单粗暴方法,就是手动添加tap
tip:1.添加代理,2.调用
- @interface XXXXXXXX ()<UIGestureRecognizerDelegate>
[self addAGesutreRecognizerForMainViewView];
//MainViewView 没有点击事件- (void)addAGesutreRecognizerForMainViewView{ UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesturedDetected:)]; tapGesture.delegate = self; [self.mainView addGestureRecognizer:tapGesture];}- (void) tapGesturedDetected:(UITapGestureRecognizer *)recognizer{ NSSet *touches=[[NSSet alloc]init]; UITouch *touch = [touches anyObject]; // 计算搜索框范围 CGPoint touchPoint = [touch locationInView:self.mainView]; NSLog(@"x = %f, y = %f", touchPoint.x, touchPoint.y); if (touchPoint.x > (self.bgImg.kkg_x+self.bgImg.kkg_width)&& touchPoint.x < self.bgImg.kkg_x && touchPoint.y > (self.bgImg.kkg_y+self.bgImg.kkg_height) && touchPoint.y < self.bgImg.kkg_y) { } else { [self closeAlert]; }}
自定义弹框,点击提示框外空白区域,让弹框消失
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。