Guest User

Untitled

a guest
Jan 16th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #import "UIAlertController+JHTapDismiss.h"
  2.  
  3. @interface UIAlertController ()<UIGestureRecognizerDelegate>
  4.  
  5. @end
  6.  
  7. @implementation UIAlertController (JHTapDismiss)
  8.  
  9. - (void)alertTapDismiss {
  10.  
  11. NSArray * arrayViews = [UIApplication sharedApplication].keyWindow.subviews;
  12. if (arrayViews.count>0) {
  13. //array会有两个对象,一个是UILayoutContainerView,另外一个是UITransitionView,我们找到最后一个
  14. UIView * backView = arrayViews.lastObject;
  15.  
  16. backView.userInteractionEnabled = YES;
  17. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap)];
  18. tap.delegate = self;
  19. [backView addGestureRecognizer:tap];
  20. }
  21.  
  22. }
  23.  
  24. - (void)tap {
  25. [self dismissViewControllerAnimated:YES completion:nil];
  26. }
  27.  
  28. #pragma mark - UIGestureRecognizerDelegate
  29. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
  30.  
  31. UIView *tapView = gestureRecognizer.view;
  32. CGPoint point = [touch locationInView:tapView];
  33. CGPoint conPoint = [self.view convertPoint:point fromView:tapView];
  34. BOOL isContains = CGRectContainsPoint(self.view.bounds, conPoint);
  35. if (isContains) {
  36. // tap点在弹出框内 不响应tap手势事件
  37. return NO;
  38. }
  39. return YES;
  40. }
  41.  
  42. @end
Add Comment
Please, Sign In to add comment