口袋优选

CCAlertShowView.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. //
  2. // CCAlertShowView.m
  3. // ProjectManager
  4. //
  5. // Created by 小花 on 2017/1/18.
  6. // Copyright © 2017年 vaic. All rights reserved.
  7. //
  8. #import "CCAlertShowView.h"
  9. #import "UIView+ScottAutoLayout.h"
  10. @interface CCAlertShowView ()
  11. @property (nonatomic, weak) UIView *alertView;
  12. @property (nonatomic, weak) UITapGestureRecognizer *tapGesture;
  13. @end
  14. @implementation CCAlertShowView
  15. - (instancetype)initWithFrame:(CGRect)frame {
  16. if (self = [super initWithFrame:frame]) {
  17. self.backgroundColor = [UIColor clearColor];
  18. _tapBackgroundDismissEnable = YES;
  19. _alertViewMargin = 15;
  20. [self addBackgroundView];
  21. [self addTapGesture];
  22. }
  23. return self;
  24. }
  25. - (void)addBackgroundView {
  26. if (_backgroundView == nil) {
  27. UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
  28. backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
  29. _backgroundView = backgroundView;
  30. }
  31. [self insertSubview:_backgroundView atIndex:0];
  32. _backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
  33. [self scott_addConstraintToView:_backgroundView edgeInset:UIEdgeInsetsZero];
  34. }
  35. - (void)addTapGesture {
  36. self.userInteractionEnabled = YES;
  37. //单指单击
  38. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureAction:)];
  39. singleTap.enabled = _tapBackgroundDismissEnable;
  40. //增加事件者响应者,
  41. [_backgroundView addGestureRecognizer:singleTap];
  42. _tapGesture = singleTap;
  43. }
  44. - (void)tapGestureAction:(UITapGestureRecognizer *)tap {
  45. if (self.tapDismissBlock) {
  46. self.tapDismissBlock();
  47. }
  48. [self dismiss];
  49. }
  50. - (instancetype)initWithAlertView:(UIView *)view {
  51. if (self = [self initWithFrame:CGRectZero]) {
  52. [self addSubview:view];
  53. _alertView = view;
  54. }
  55. return self;
  56. }
  57. + (instancetype)alertViewWithView:(UIView *)view {
  58. return [[self alloc] initWithAlertView:view];
  59. }
  60. + (instancetype)showAlertViewWithView:(UIView *)alertView backgroundDismissEnable:(BOOL)backgroundDismissEnable {
  61. CCAlertShowView *showAlertView = [self alertViewWithView:alertView];
  62. showAlertView.tapBackgroundDismissEnable = backgroundDismissEnable;
  63. [showAlertView show];
  64. return showAlertView;
  65. }
  66. - (void)didMoveToSuperview {
  67. if (self.superview) {
  68. self.translatesAutoresizingMaskIntoConstraints = NO;
  69. [self.superview scott_addConstraintToView:self edgeInset:UIEdgeInsetsZero];
  70. [self layoutAlertView];
  71. }
  72. }
  73. - (void)layoutAlertView {
  74. _alertView.translatesAutoresizingMaskIntoConstraints = NO;
  75. // center X
  76. [self scott_addConstraintCenterXToView:_alertView constant:0];
  77. // width, height
  78. if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
  79. [_alertView scott_addConstraintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
  80. }else {
  81. BOOL findAlertViewWidthConstraint = NO;
  82. for (NSLayoutConstraint *constraint in _alertView.constraints) {
  83. if (constraint.firstAttribute == NSLayoutAttributeWidth) {
  84. findAlertViewWidthConstraint = YES;
  85. break;
  86. }
  87. }
  88. if (!findAlertViewWidthConstraint) {
  89. CGFloat width = CGRectGetWidth(self.superview.frame) - (2 * _alertViewMargin);
  90. [_alertView scott_addConstraintWidth:width height:0];
  91. }
  92. }
  93. // topY
  94. NSLayoutConstraint *alertViewCenterYConstraint = [self scott_addConstraintCenterYToView:_alertView constant:0];
  95. if (_alertOriginY > 0) {
  96. [_alertView layoutIfNeeded];
  97. alertViewCenterYConstraint.constant = _alertOriginY - (CGRectGetHeight(self.superview.frame) - CGRectGetHeight(_alertView.frame))/2;
  98. }
  99. }
  100. - (void)setTapBackgroundDismissEnable:(BOOL)tapBackgroundDismissEnable {
  101. _tapBackgroundDismissEnable = tapBackgroundDismissEnable;
  102. _tapGesture.enabled = tapBackgroundDismissEnable;
  103. }
  104. - (void)show {
  105. if (self.superview == nil) {
  106. [[UIApplication sharedApplication].keyWindow addSubview:self];
  107. }
  108. self.alpha = 0;
  109. _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
  110. [UIView animateWithDuration:0.3 animations:^{
  111. _alertView.transform = CGAffineTransformIdentity;
  112. self.alpha = 1;
  113. }];
  114. }
  115. - (void)showInView:(UIView *)superView {
  116. [superView addSubview:self];
  117. self.alpha = 0;
  118. _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
  119. [UIView animateWithDuration:0.3 animations:^{
  120. _alertView.transform = CGAffineTransformIdentity;
  121. self.alpha = 1;
  122. }];
  123. }
  124. - (void)dismiss {
  125. if (self.superview) {
  126. [UIView animateWithDuration:0.3 animations:^{
  127. _alertView.transform = CGAffineTransformScale(_alertView.transform, 0.1, 0.1);
  128. _alertView.alpha = 0;
  129. } completion:^(BOOL finished) {
  130. [self removeFromSuperview];
  131. }];
  132. }
  133. }
  134. @end