口袋优选

TYShowAlertView.m 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // TYShowAlertView.m
  3. // TYAlertControllerDemo
  4. //
  5. // Created by tanyang on 15/3/16.
  6. // Copyright (c) 2015年 mark. All rights reserved.
  7. //
  8. #import "TYShowAlertView.h"
  9. #import "UIView+TYAutoLayout.h"
  10. @interface TYShowAlertView ()
  11. @property (nonatomic, weak) UIView *alertView;
  12. @property (nonatomic, weak) UITapGestureRecognizer *singleTap;
  13. @end
  14. //current window
  15. #define kCurrentWindow [[UIApplication sharedApplication].windows firstObject]
  16. @implementation TYShowAlertView
  17. - (instancetype)initWithFrame:(CGRect)frame
  18. {
  19. if (self = [super initWithFrame:frame]) {
  20. self.backgroundColor = [UIColor clearColor];
  21. _backgoundTapDismissEnable = NO;
  22. _alertViewEdging = 15;
  23. [self addBackgroundView];
  24. [self addSingleGesture];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithAlertView:(UIView *)tipView
  29. {
  30. if (self = [self initWithFrame:CGRectZero]) {
  31. [self addSubview:tipView];
  32. _alertView = tipView;
  33. }
  34. return self;
  35. }
  36. + (instancetype)alertViewWithView:(UIView *)tipView
  37. {
  38. return [[self alloc]initWithAlertView:tipView];
  39. }
  40. + (void)showAlertViewWithView:(UIView *)alertView
  41. {
  42. [self showAlertViewWithView:alertView backgoundTapDismissEnable:NO];
  43. }
  44. + (void)showAlertViewWithView:(UIView *)alertView backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  45. {
  46. TYShowAlertView *showTipView = [self alertViewWithView:alertView];
  47. showTipView.backgoundTapDismissEnable = backgoundTapDismissEnable;
  48. [showTipView show];
  49. }
  50. + (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY
  51. {
  52. [self showAlertViewWithView:alertView
  53. originY:originY backgoundTapDismissEnable:NO];
  54. }
  55. + (void)showAlertViewWithView:(UIView *)alertView originY:(CGFloat)originY backgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  56. {
  57. TYShowAlertView *showTipView = [self alertViewWithView:alertView];
  58. showTipView.alertViewOriginY = originY;
  59. showTipView.backgoundTapDismissEnable = backgoundTapDismissEnable;
  60. [showTipView show];
  61. }
  62. - (void)addBackgroundView
  63. {
  64. if (_backgroundView == nil) {
  65. UIView *backgroundView = [[UIView alloc]initWithFrame:self.bounds];
  66. backgroundView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
  67. _backgroundView = backgroundView;
  68. }
  69. [self insertSubview:_backgroundView atIndex:0];
  70. _backgroundView.translatesAutoresizingMaskIntoConstraints = NO;
  71. [self addConstraintToView:_backgroundView edgeInset:UIEdgeInsetsZero];
  72. }
  73. - (void)setBackgroundView:(UIView *)backgroundView
  74. {
  75. if (_backgroundView != backgroundView) {
  76. [_backgroundView removeFromSuperview];
  77. _backgroundView = backgroundView;
  78. [self addBackgroundView];
  79. [self addSingleGesture];
  80. }
  81. }
  82. - (void)setBackgoundTapDismissEnable:(BOOL)backgoundTapDismissEnable
  83. {
  84. _backgoundTapDismissEnable = backgoundTapDismissEnable;
  85. _singleTap.enabled = backgoundTapDismissEnable;
  86. }
  87. - (void)didMoveToSuperview
  88. {
  89. if (self.superview) {
  90. self.translatesAutoresizingMaskIntoConstraints = NO;
  91. [self.superview addConstraintToView:self edgeInset:UIEdgeInsetsZero];
  92. [self layoutAlertView];
  93. }
  94. }
  95. - (void)layoutAlertView
  96. {
  97. _alertView.translatesAutoresizingMaskIntoConstraints = NO;
  98. // center X
  99. [self addConstraintCenterXToView:_alertView centerYToView:nil];
  100. // width, height
  101. if (!CGSizeEqualToSize(_alertView.frame.size,CGSizeZero)) {
  102. [_alertView addConstraintWidth:CGRectGetWidth(_alertView.frame) height:CGRectGetHeight(_alertView.frame)];
  103. }else {
  104. BOOL findAlertViewWidthConstraint = NO;
  105. for (NSLayoutConstraint *constraint in _alertView.constraints) {
  106. if (constraint.firstAttribute == NSLayoutAttributeWidth) {
  107. findAlertViewWidthConstraint = YES;
  108. break;
  109. }
  110. }
  111. if (!findAlertViewWidthConstraint) {
  112. [_alertView addConstraintWidth:CGRectGetWidth(self.superview.frame)-2*_alertViewEdging height:0];
  113. }
  114. }
  115. // topY
  116. NSLayoutConstraint *alertViewCenterYConstraint = [self addConstraintCenterYToView:_alertView constant:0];
  117. if (_alertViewOriginY > 0) {
  118. [_alertView layoutIfNeeded];
  119. alertViewCenterYConstraint.constant = _alertViewOriginY - (CGRectGetHeight(self.superview.frame) - CGRectGetHeight(_alertView.frame))/2;
  120. }
  121. }
  122. #pragma mark - add Gesture
  123. - (void)addSingleGesture
  124. {
  125. self.userInteractionEnabled = YES;
  126. //单指单击
  127. UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTap:)];
  128. singleTap.enabled = _backgoundTapDismissEnable;
  129. //增加事件者响应者,
  130. [_backgroundView addGestureRecognizer:singleTap];
  131. _singleTap = singleTap;
  132. }
  133. #pragma mark 手指点击事件
  134. - (void)singleTap:(UITapGestureRecognizer *)sender
  135. {
  136. [self hide];
  137. }
  138. - (void)show
  139. {
  140. if (self.superview == nil) {
  141. [kCurrentWindow addSubview:self];
  142. }
  143. self.alpha = 0;
  144. _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
  145. [UIView animateWithDuration:0.3 animations:^{
  146. _alertView.transform = CGAffineTransformIdentity;
  147. self.alpha = 1;
  148. }];
  149. }
  150. - (void)hide
  151. {
  152. if (self.superview) {
  153. [UIView animateWithDuration:0.3 animations:^{
  154. _alertView.transform = CGAffineTransformScale(_alertView.transform,0.1,0.1);
  155. self.alpha = 0;
  156. } completion:^(BOOL finished) {
  157. [self removeFromSuperview];
  158. }];
  159. }
  160. }
  161. - (void)dealloc
  162. {
  163. NSLog(@"%@ dealloc",NSStringFromClass([self class]));
  164. }
  165. @end