MMPopupWindow.m 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // MMPopupWindow.m
  3. // MMPopupView
  4. //
  5. // Created by Ralph Li on 9/6/15.
  6. // Copyright © 2015 LJC. All rights reserved.
  7. //
  8. #import "MMPopupWindow.h"
  9. #import "MMPopupCategory.h"
  10. #import "MMPopupDefine.h"
  11. #import "MMPopupView.h"
  12. @interface MMPopupWindow()
  13. <
  14. UIGestureRecognizerDelegate
  15. >
  16. @end
  17. @implementation MMPopupWindow
  18. - (instancetype)initWithFrame:(CGRect)frame
  19. {
  20. self = [super initWithFrame:frame];
  21. if ( self )
  22. {
  23. self.windowLevel = UIWindowLevelStatusBar + 1;
  24. UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionTap:)];
  25. gesture.cancelsTouchesInView = NO;
  26. gesture.delegate = self;
  27. [self addGestureRecognizer:gesture];
  28. }
  29. return self;
  30. }
  31. + (MMPopupWindow *)sharedWindow
  32. {
  33. static MMPopupWindow *window;
  34. static dispatch_once_t onceToken;
  35. dispatch_once(&onceToken, ^{
  36. window = [[MMPopupWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  37. window.rootViewController = [UIViewController new];
  38. });
  39. return window;
  40. }
  41. - (void)cacheWindow
  42. {
  43. [self makeKeyAndVisible];
  44. [[[UIApplication sharedApplication].delegate window] makeKeyAndVisible];
  45. [self attachView].mm_dimBackgroundView.hidden = YES;
  46. self.hidden = YES;
  47. }
  48. - (void)actionTap:(UITapGestureRecognizer*)gesture
  49. {
  50. if ( self.touchWildToHide && !self.mm_dimBackgroundAnimating )
  51. {
  52. for ( UIView *v in [self attachView].mm_dimBackgroundView.subviews )
  53. {
  54. if ( [v isKindOfClass:[MMPopupView class]] )
  55. {
  56. MMPopupView *popupView = (MMPopupView*)v;
  57. [popupView hide];
  58. }
  59. }
  60. }
  61. }
  62. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
  63. {
  64. return ( touch.view == self.attachView.mm_dimBackgroundView );
  65. }
  66. - (UIView *)attachView
  67. {
  68. return self.rootViewController.view;
  69. }
  70. @end