省钱达人

DXAlertView.m 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // DXAlertView.m
  3. // Elephant
  4. //
  5. // Created by dyy on 2018/1/19.
  6. // Copyright © 2018年 dyy. All rights reserved.
  7. //
  8. #define AlertView_W HitoActureWidth(260.f)
  9. #define DXATitle_H 20.0f
  10. #define MessageMin_H 80.0f //messagelab的最小高度
  11. #define DXABtn_H 35.0f
  12. //屏幕宽度与高度
  13. #define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
  14. #define SCREENH_HEIGHT [UIScreen mainScreen].bounds.size.height
  15. //比例宽和高(以6为除数)
  16. #define HitoActureHeight(height) roundf(height/375.0 * SCREEN_WIDTH)
  17. #define HitoActureWidth(Width) roundf(Width/667.0 * SCREENH_HEIGHT)
  18. #define FONT_17 [UIScreen mainScreen].bounds.size.width > 320 ? [UIFont systemFontOfSize:17.f] : [UIFont systemFontOfSize:13.5f]
  19. #define FONT_15 [UIScreen mainScreen].bounds.size.width > 320 ? [UIFont systemFontOfSize:15.f] : [UIFont systemFontOfSize:12.f]
  20. #define MAIN_COLOR ColorRGBA(118, 216, 206, 1)
  21. #define ColorRGBA(r, g, b, a) ([UIColor colorWithRed:(r)/255.0 green:(g)/255.0 blue:(b)/255.0 alpha:(a)])
  22. #import "DXAlertView.h"
  23. #import "UILabel+ChangeLineSpaceAndWordSpace.h"
  24. @interface DXAlertView()<UIGestureRecognizerDelegate>
  25. @property (nonatomic,strong) UIView *alertview;
  26. @property (nonatomic,strong)UILabel *titleLab;
  27. @property (nonatomic,strong)UILabel *messageLab;
  28. @property (nonatomic,strong)UIButton *cancelBtn;
  29. @property (nonatomic,strong)UIButton *otherBtn;
  30. @end
  31. @implementation DXAlertView
  32. -(instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelBtnTitle:(NSString *)cancelTitle otherBtnTitle:(NSString *)otherBtnTitle
  33. {
  34. self = [super init];
  35. if (self) {
  36. self.frame = [UIScreen mainScreen].bounds;
  37. if (title) {
  38. self.titleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, AlertView_W, DXATitle_H)];
  39. self.titleLab.text=title;
  40. self.titleLab.textAlignment=NSTextAlignmentCenter;
  41. self.titleLab.textColor=[UIColor blackColor];
  42. self.titleLab.font=FONT_17;
  43. }
  44. CGFloat messageLabSpace = 15;
  45. self.messageLab=[[UILabel alloc] init];
  46. self.messageLab.backgroundColor=[UIColor whiteColor];
  47. self.messageLab.text=message;
  48. self.messageLab.textColor=[UIColor lightGrayColor];
  49. self.messageLab.font=FONT_15;
  50. self.messageLab.numberOfLines=0;
  51. self.messageLab.textAlignment=NSTextAlignmentCenter;
  52. self.messageLab.lineBreakMode=NSLineBreakByTruncatingTail;
  53. self.messageLab.characterSpace=1;
  54. self.messageLab.lineSpace=2;
  55. CGSize labSize = [self.messageLab getLableRectWithMaxWidth:AlertView_W-messageLabSpace*2];
  56. CGFloat messageLabAotuH = labSize.height < MessageMin_H?MessageMin_H:labSize.height;
  57. self.messageLab.frame=CGRectMake(messageLabSpace, _titleLab.frame.size.height+self.titleLab.frame.origin.y +10, AlertView_W-messageLabSpace*2, messageLabAotuH);
  58. if (cancelTitle) {
  59. self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  60. [self.cancelBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
  61. [self.cancelBtn setTitle:cancelTitle forState:UIControlStateNormal];
  62. self.cancelBtn.titleLabel.font=FONT_15;
  63. self.cancelBtn.layer.cornerRadius=3;
  64. self.cancelBtn.layer.masksToBounds=YES;
  65. self.cancelBtn.backgroundColor = [UIColor groupTableViewBackgroundColor];
  66. [self.cancelBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  67. [self.alertview addSubview:self.cancelBtn];
  68. }
  69. if (otherBtnTitle) {
  70. self.otherBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  71. [self.otherBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  72. [self.otherBtn setTitle:otherBtnTitle forState:UIControlStateNormal];
  73. self.otherBtn.titleLabel.font=FONT_15;
  74. self.otherBtn.layer.cornerRadius=3;
  75. self.otherBtn.layer.masksToBounds=YES;
  76. self.otherBtn.backgroundColor = MAIN_COLOR;
  77. [self.otherBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
  78. [self.alertview addSubview:self.otherBtn];
  79. }
  80. CGFloat btn_y = _titleLab.frame.size.height+10 + +self.titleLab.frame.origin.y + self.messageLab.frame.size.height +10;
  81. if (cancelTitle && !otherBtnTitle) {
  82. self.cancelBtn.tag=0;
  83. self.cancelBtn.frame =CGRectMake(15,btn_y, AlertView_W-30, DXABtn_H);
  84. }
  85. else if (!cancelTitle && otherBtnTitle)
  86. {
  87. self.otherBtn.tag=1;
  88. self.otherBtn.frame=CGRectMake(15,btn_y, AlertView_W-30, DXABtn_H);
  89. }
  90. else if (cancelTitle && otherBtnTitle)
  91. {
  92. _cancelBtn.tag=0;
  93. _otherBtn.tag=1;
  94. CGFloat btnSpace = 20;//两个btn之间的间距
  95. CGFloat btn_w =(AlertView_W-15*2-btnSpace)/2;
  96. _cancelBtn.frame=CGRectMake(15, btn_y, btn_w, DXABtn_H);
  97. _otherBtn.frame=CGRectMake(btn_w+15+20, btn_y, btn_w, DXABtn_H);
  98. }
  99. self.alertview.frame = CGRectMake(0, 0, AlertView_W, _titleLab.frame.size.height+10 + +self.titleLab.frame.origin.y + self.messageLab.frame.size.height +10 + DXABtn_H + 10);
  100. self.alertview.center = self.center;
  101. [self addSubview:self.alertview];
  102. [self.alertview addSubview:self.titleLab];
  103. [self.alertview addSubview:self.messageLab];
  104. }
  105. return self;
  106. }
  107. -(UIView *)alertview
  108. {
  109. if (_alertview == nil) {
  110. _alertview = [[UIView alloc] init];
  111. _alertview.backgroundColor = [UIColor whiteColor];
  112. _alertview.layer.cornerRadius=5.0;
  113. _alertview.layer.masksToBounds=YES;
  114. _alertview.userInteractionEnabled=YES;
  115. }
  116. return _alertview;
  117. }
  118. -(void)show
  119. {
  120. self.backgroundColor = [UIColor clearColor];
  121. [[UIApplication sharedApplication].keyWindow addSubview:self];
  122. CGAffineTransform transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
  123. self.alertview.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.2,0.2);
  124. self.alertview.alpha = 0;
  125. [UIView animateWithDuration:0.3 delay:0.1 usingSpringWithDamping:0.5 initialSpringVelocity:10 options:UIViewAnimationOptionCurveLinear animations:^{
  126. self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:.4f];
  127. self.alertview.transform = transform;
  128. self.alertview.alpha = 1;
  129. } completion:^(BOOL finished) {
  130. }];
  131. }
  132. -(void)dismissAlertView{
  133. [UIView animateWithDuration:0.3 animations:^{
  134. [self removeFromSuperview];
  135. }];
  136. }
  137. -(void)btnClick:(UIButton *)btn{
  138. if (self.clickBlock) {
  139. self.clickBlock(btn.tag);
  140. [self dismissAlertView];
  141. return;
  142. }
  143. if ([self.delegate respondsToSelector:@selector(dxAlertView:clickedButtonAtIndex:)]) {
  144. [self.delegate dxAlertView:self clickedButtonAtIndex:btn.tag];
  145. }
  146. [self dismissAlertView];
  147. }
  148. #pragma mark UIGestureRecognizerDelegate
  149. - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
  150. if ([touch.view isDescendantOfView:self.alertview]) {
  151. return NO;
  152. }
  153. return YES;
  154. }
  155. @end