给UIAlertView添加手势使点击区域外弹框消失
2015-11-26 14:06
453 查看
给UIAlertView添加手势,点击alertView区域外使alertView消失。
方法解释:
添加注释的那一行是因为alertView本身是没有点击事件的,加上手势之后也就只有一个点击事件,因此无所谓要不要限制触摸事件的传递。参考cancelsTouchesInView初见
(BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; // 判断point是否在接收者(此处指alertView)的区域内,一般是在的(返回为YES)
(CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view; //将一个点转换为接收者,这个点位于给定的视图所在的坐标系中
(CGPoint)locationInView:(nullable UIView*)view; // 单点手势的位置,一般取的是触摸点的中心的位置。
后来发现:点击alertView按钮时,对于iOS7及以下的系统会直接触发手势响应方法,从而不再调用alertView的代理方法。
因此修改了以上代码。
补充:
- (void)disappearAlertView { UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideAlertView:)]; // tap.cancelsTouchesInView = NO; [[UIApplication sharedApplication].keyWindow addGestureRecognizer:tap]; } - (void)hideAlertView:(UITapGestureRecognizer *)tap { if (tap.state == UIGestureRecognizerStateEnded){ CGPoint location = [tap locationInView:nil]; if (![self.alertView pointInside:[self.alertView convertPoint:location fromView:self.alertView.window] withEvent:nil]){ [self.alertView.window removeGestureRecognizer:tap]; [self.alertView dismissWithClickedButtonIndex:0 animated:YES]; } } }
方法解释:
添加注释的那一行是因为alertView本身是没有点击事件的,加上手势之后也就只有一个点击事件,因此无所谓要不要限制触摸事件的传递。参考cancelsTouchesInView初见
(BOOL)pointInside:(CGPoint)point withEvent:(nullable UIEvent *)event; // 判断point是否在接收者(此处指alertView)的区域内,一般是在的(返回为YES)
(CGPoint)convertPoint:(CGPoint)point fromView:(nullable UIView *)view; //将一个点转换为接收者,这个点位于给定的视图所在的坐标系中
(CGPoint)locationInView:(nullable UIView*)view; // 单点手势的位置,一般取的是触摸点的中心的位置。
后来发现:点击alertView按钮时,对于iOS7及以下的系统会直接触发手势响应方法,从而不再调用alertView的代理方法。
因此修改了以上代码。
/** * 需求:点击弹框之外的区域使得弹框消失 * 实施:给keyWindow添加手势,使得点击弹框之外的区域时弹框消失 */ - (void)buttonAlertView:(UIButton *)btn { self.alertView = [[UIAlertView alloc] initWithTitle:nil message:@"是否放弃此次操作?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [self.alertView show]; self.recognizerTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapBehind:)]; self.recognizerTap.cancelsTouchesInView = NO; [[UIApplication sharedApplication].keyWindow addGestureRecognizer:self.recognizerTap]; } //手势响应方法-->使得弹框消失 - (void)handleTapBehind:(UITapGestureRecognizer *)sender { if (sender.state == UIGestureRecognizerStateEnded) { if (IOS8DEVICE) { [self removeTap]; } else { //点击alertView按钮时,对于iOS7及以下的系统会直接触发手势响应方法,从而不再调用alertView的代理方法。因此延迟0.1s执行移除手势方法,即点击alertView按钮时能正确调用alertView的代理方法 [self performSelector:@selector(removeTap) withObject:nil afterDelay:0.1f]; } } } //移除手势 - (void)removeTap { [self.alertView.window removeGestureRecognizer:self.recognizerTap]; [self.alertView dismissWithClickedButtonIndex:0 animated:YES]; }
补充:
#define IOS8DEVICE [[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0
相关文章推荐
- JS控制一个DIV层在指定时间内消失的方法
- Source Insight的Context Window消失了如何处理?
- 青春痘真的消失不见了
- ubuntu 12.04 任务栏消失 解决办法
- myEclipse中的项目导航栏和代码编辑栏不见了
- 提示框与动画的结合使用
- UIAlertView
- 控制AlertDialog的不可消失状态
- 模态弹窗,点击黑色半透明区域消失
- SurfaceView中的PNG格式图片隐藏不见的原因-背景覆盖
- Vs2010使用SplitterWnd时,拖动窗口导致View间歇性消失
- 开发日记(5) 我们如何让EditText的光标消失呢?
- UIAlertView显示后自动消失
- UIAlertView
- 电脑强制休眠后独显消失了?
- 回忆,更多的只是回忆!
- 【教你快速删掉XP系统历史记录】
- win7运行bat文件 一闪而过 解决
- 3dmax 蒙皮 消失
- iOS学习(6)——UIAlertView