《省钱达人》与《猎豆优选》UI相同版。域名tbk

CCAlertShowView.m 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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.4];
  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. [self dismiss];
  46. }
  47. - (instancetype)initWithAlertView:(UIView *)view {
  48. if (self = [self initWithFrame:CGRectZero]) {
  49. [self addSubview:view];
  50. _alertView = view;
  51. }
  52. return self;
  53. }
  54. + (instancetype)alertViewWithView:(UIView *)view {
  55. return [[self alloc] initWithAlertView:view];
  56. }
  57. + (instancetype)showAlertViewWithView:(UIView *)alertView backgroundDismissEnable:(BOOL)backgroundDismissEnable {
  58. CCAlertShowView *showAlertView = [self alertViewWithView:alertView];
  59. showAlertView.tapBackgroundDismissEnable = backgroundDismissEnable;
  60. [showAlertView show];
  61. return showAlertView;
  62. }
  63. - (void)didMoveToSuperview {
  64. if (self.superview) {
  65. self.translatesAutoresizingMaskIntoConstraints = NO;
  66. [self.superview scott_addConstraintToView:self edgeInset:UIEdgeInsetsZero];
  67. [self layoutAlertView];
  68. }
  69. }
  70. - (void)layoutAlertView {
  71. _alertView.translatesAutoresizingMaskIntoConstraints = NO;
  72. // center X
  73. [self scott_addConstraintCenterXToView:_alertView constant:0];
  74. // width, height
  75. if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
  76. [_alertView scott_addConstraintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
  77. }else {
  78. BOOL findAlertViewWidthConstraint = NO;
  79. for (NSLayoutConstraint *constraint in _alertView.constraints) {
  80. if (constraint.firstAttribute == NSLayoutAttributeWidth) {
  81. findAlertViewWidthConstraint = YES;
  82. break;
  83. }
  84. }
  85. if (!findAlertViewWidthConstraint) {
  86. CGFloat width = CGRectGetWidth(self.superview.frame) - (2 * _alertViewMargin);
  87. [_alertView scott_addConstraintWidth:width height:0];
  88. }
  89. }
  90. // topY
  91. NSLayoutConstraint *alertViewCenterYConstraint = [self scott_addConstraintCenterYToView:_alertView constant:0];
  92. if (_alertOriginY > 0) {
  93. [_alertView layoutIfNeeded];
  94. alertViewCenterYConstraint.constant = _alertOriginY - (CGRectGetHeight(self.superview.frame) - CGRectGetHeight(_alertView.frame))/2;
  95. }
  96. }
  97. - (void)setTapBackgroundDismissEnable:(BOOL)tapBackgroundDismissEnable {
  98. _tapBackgroundDismissEnable = tapBackgroundDismissEnable;
  99. _tapGesture.enabled = tapBackgroundDismissEnable;
  100. }
  101. - (void)show {
  102. if (self.superview == nil) {
  103. [[UIApplication sharedApplication].keyWindow addSubview:self];
  104. }
  105. self.alpha = 0;
  106. _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
  107. [UIView animateWithDuration:0.3 animations:^{
  108. _alertView.transform = CGAffineTransformIdentity;
  109. self.alpha = 1;
  110. }];
  111. }
  112. - (void)showInView:(UIView *)superView {
  113. [superView addSubview:self];
  114. self.alpha = 0;
  115. _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
  116. [UIView animateWithDuration:0.3 animations:^{
  117. _alertView.transform = CGAffineTransformIdentity;
  118. self.alpha = 1;
  119. }];
  120. }
  121. - (void)dismiss {
  122. if (self.superview) {
  123. [UIView animateWithDuration:0.3 animations:^{
  124. _alertView.transform = CGAffineTransformScale(_alertView.transform, 0.1, 0.1);
  125. _alertView.alpha = 0;
  126. } completion:^(BOOL finished) {
  127. _alertView.transform = CGAffineTransformIdentity;
  128. _alertView.alpha = 1;
  129. [self removeFromSuperview];
  130. }];
  131. }
  132. }
  133. @end