Skip to content

Instantly share code, notes, and snippets.

@dhl613
Created January 16, 2019 08:20
Show Gist options
  • Save dhl613/d4a2a14d57330043e873ba035a684800 to your computer and use it in GitHub Desktop.
Save dhl613/d4a2a14d57330043e873ba035a684800 to your computer and use it in GitHub Desktop.
UIAlertController添加点击空白dismiss手势
#import "UIAlertController+JHTapDismiss.h"
@interface UIAlertController ()<UIGestureRecognizerDelegate>
@end
@implementation UIAlertController (JHTapDismiss)
- (void)alertTapDismiss {
NSArray * arrayViews = [UIApplication sharedApplication].keyWindow.subviews;
if (arrayViews.count>0) {
//array会有两个对象,一个是UILayoutContainerView,另外一个是UITransitionView,我们找到最后一个
UIView * backView = arrayViews.lastObject;
backView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
tap.delegate = self;
[backView addGestureRecognizer:tap];
}
}
- (void)tap {
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
UIView *tapView = gestureRecognizer.view;
CGPoint point = [touch locationInView:tapView];
CGPoint conPoint = [self.view convertPoint:point fromView:tapView];
BOOL isContains = CGRectContainsPoint(self.view.bounds, conPoint);
if (isContains) {
// tap点在弹出框内 不响应tap手势事件
return NO;
}
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment