KSNormalAlertView.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // KSNormalAlertView.m
  3. // StudentDaya
  4. //
  5. // Created by Kyle on 2020/6/15.
  6. // Copyright © 2020 DayaMusic. All rights reserved.
  7. //
  8. #import "KSNormalAlertView.h"
  9. typedef enum : NSUInteger {
  10. ClassRoomAlertViewCancel,
  11. ClassRoomAlertViewConfirm,
  12. } ClassRoomAlertViewActionTag;
  13. #define AWidth 320
  14. #define AHeight 160
  15. @interface KSNormalAlertView ()
  16. @property (nonatomic, strong) UILabel *tipsLabel;
  17. @property (nonatomic, strong) UILabel *titleLable;
  18. @property (nonatomic, strong) UIButton *cancelButton;
  19. @property (nonatomic, strong) UIButton *sureButton;
  20. @property (nonatomic, strong) NSString *title;
  21. @property (nonatomic, strong) NSString *leftTitle;
  22. @property (nonatomic, strong) NSString *rightTitle;
  23. @property (nonatomic, copy) ButtonCallback cancel;
  24. @property (nonatomic, copy) ButtonCallback confirm;
  25. @end
  26. @implementation KSNormalAlertView
  27. + (void)ks_showAlertWithTitle:(NSString *)title confirmTitle:(NSString *)confirmTitle confirm:(ButtonCallback)confirm {
  28. KSNormalAlertView * alertView = [[KSNormalAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  29. alertView.backgroundColor = HexRGBAlpha(0x000000, 0.5f);
  30. alertView.title = title;
  31. alertView.rightTitle = confirmTitle;
  32. alertView.confirm = confirm;
  33. [alertView addCancelSubview];
  34. [alertView showAlertView];
  35. }
  36. + (void)ks_showAlertWithTitle:(NSString *)title leftTitle:(NSString *)leftTitle rightTitle:(NSString *)rightTitle cancel:(ButtonCallback)cancel confirm:(ButtonCallback)confirm {
  37. KSNormalAlertView * alertView = [[KSNormalAlertView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  38. alertView.backgroundColor = HexRGBAlpha(0x000000, 0.5f);
  39. alertView.title = title;
  40. alertView.leftTitle = leftTitle;
  41. alertView.rightTitle = rightTitle;
  42. alertView.cancel = cancel;
  43. alertView.confirm =confirm;
  44. [alertView addSubviews];
  45. [alertView showAlertView];
  46. }
  47. - (void)showAlertView {
  48. [[UIApplication sharedApplication].keyWindow addSubview:self];
  49. }
  50. -(void)dismissAlertView{
  51. [self removeFromSuperview];
  52. }
  53. - (void)addCancelSubview {
  54. UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake((UIScreenWidth - AWidth) / 2, (UIScreenHeight - AHeight) / 2, AWidth, AHeight)];
  55. contentView.backgroundColor = HexRGB(0x161616);
  56. contentView.layer.cornerRadius = 8;
  57. contentView.layer.masksToBounds = YES;
  58. [self addSubview:contentView];
  59. UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AWidth, 38)];
  60. topView.backgroundColor = HexRGB(0x222327);
  61. [contentView addSubview:topView];
  62. [topView addSubview:self.tipsLabel];
  63. [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  64. make.left.mas_equalTo(topView.mas_left).offset(13);
  65. make.top.bottom.mas_equalTo(topView);
  66. make.right.mas_equalTo(topView.mas_right).offset(-13);
  67. }];
  68. [contentView addSubview:self.titleLable];
  69. [contentView addSubview:self.sureButton];
  70. [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.top.equalTo(topView.mas_top).offset(20);
  72. make.left.equalTo(contentView.mas_left).offset(13);
  73. make.right.equalTo(contentView.mas_right).offset(-13);
  74. make.bottom.equalTo(self.sureButton.mas_bottom).offset(-15);
  75. }];
  76. [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
  77. make.bottom.mas_equalTo(contentView.mas_bottom).offset(-16);
  78. make.height.mas_equalTo(26);
  79. make.width.mas_equalTo(100);
  80. make.centerX.mas_equalTo(contentView.mas_centerX);
  81. }];
  82. }
  83. - (void)addSubviews {
  84. UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake((UIScreenWidth - AWidth) / 2, (UIScreenHeight - AHeight) / 2, AWidth, AHeight)];
  85. contentView.backgroundColor = HexRGB(0x161616);
  86. contentView.layer.cornerRadius = 8;
  87. contentView.layer.masksToBounds = YES;
  88. [self addSubview:contentView];
  89. UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, AWidth, 38)];
  90. topView.backgroundColor = HexRGB(0x222327);
  91. [contentView addSubview:topView];
  92. [topView addSubview:self.tipsLabel];
  93. [self.tipsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  94. make.left.mas_equalTo(topView.mas_left).offset(13);
  95. make.top.bottom.mas_equalTo(topView);
  96. make.right.mas_equalTo(topView.mas_right).offset(-13);
  97. }];
  98. [contentView addSubview:self.titleLable];
  99. [contentView addSubview:self.cancelButton];
  100. [contentView addSubview:self.sureButton];
  101. [self.titleLable mas_makeConstraints:^(MASConstraintMaker *make) {
  102. make.top.equalTo(topView.mas_top).offset(20);
  103. make.left.equalTo(contentView.mas_left).offset(13);
  104. make.right.equalTo(contentView.mas_right).offset(-13);
  105. make.bottom.equalTo(self.sureButton.mas_bottom).offset(-15);
  106. }];
  107. [self.cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
  108. make.left.mas_equalTo(AWidth/4.0 - 50);
  109. make.bottom.equalTo(contentView.mas_bottom).offset(-16);
  110. make.height.mas_equalTo(26);
  111. make.width.mas_equalTo(100);
  112. }];
  113. [self.sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
  114. make.left.mas_equalTo(AWidth * 3/4.0 -50);
  115. make.bottom.mas_equalTo(contentView.mas_bottom).offset(-16);
  116. make.height.mas_equalTo(26);
  117. make.width.mas_equalTo(100);
  118. }];
  119. }
  120. - (void)buttonAction:(UIButton *)button {
  121. if (button.tag == ClassRoomAlertViewCancel) {
  122. self.cancel();
  123. }else {
  124. self.confirm();
  125. }
  126. [self dismissAlertView];
  127. }
  128. - (UILabel *)tipsLabel {
  129. if (!_tipsLabel) {
  130. _tipsLabel = [[UILabel alloc] init];
  131. _tipsLabel.font = [UIFont systemFontOfSize:16];
  132. _tipsLabel.textAlignment = NSTextAlignmentLeft;
  133. _tipsLabel.text = @"提示!";
  134. _tipsLabel.numberOfLines = 1;
  135. _tipsLabel.lineBreakMode = NSLineBreakByTruncatingMiddle;
  136. _tipsLabel.textColor = HexRGB(0xf9f9f9);
  137. }
  138. return _tipsLabel;
  139. }
  140. - (UILabel *)titleLable {
  141. if (!_titleLable) {
  142. _titleLable = [[UILabel alloc] init];
  143. _titleLable.font = [UIFont systemFontOfSize:16];
  144. _titleLable.textAlignment = NSTextAlignmentLeft;
  145. _titleLable.text = self.title;
  146. _titleLable.numberOfLines = 0;
  147. _titleLable.lineBreakMode = NSLineBreakByTruncatingMiddle;
  148. _titleLable.textColor = HexRGB(0xf9f9f9);
  149. }
  150. return _titleLable;
  151. }
  152. - (UIButton *)cancelButton {
  153. if(!_cancelButton) {
  154. _cancelButton = [[UIButton alloc] init];
  155. _cancelButton.backgroundColor = [UIColor clearColor];
  156. [_cancelButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
  157. [_cancelButton setTitleColor:HexRGB(0x08f4e6) forState:UIControlStateNormal];
  158. [_cancelButton setTitle:self.leftTitle forState:UIControlStateNormal];
  159. _cancelButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  160. _cancelButton.tag = ClassRoomAlertViewCancel;
  161. _cancelButton.layer.cornerRadius = 13.0f;
  162. _cancelButton.layer.borderWidth = 1.0;
  163. _cancelButton.layer.borderColor = HexRGB(0x08f4e6).CGColor;
  164. [_cancelButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  165. }
  166. return _cancelButton;
  167. }
  168. - (UIButton *)sureButton {
  169. if (!_sureButton) {
  170. _sureButton = [[UIButton alloc] init];
  171. _sureButton.backgroundColor = [UIColor clearColor];
  172. [_sureButton.titleLabel setFont:[UIFont systemFontOfSize:16]];
  173. [_sureButton setTitleColor:HexRGB(0x08f4e6) forState:UIControlStateNormal];
  174. [_sureButton setTitle:self.rightTitle forState:UIControlStateNormal];
  175. _sureButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  176. _sureButton.tag = ClassRoomAlertViewConfirm;
  177. _sureButton.layer.cornerRadius = 13.0f;
  178. _sureButton.layer.borderWidth = 1.0;
  179. _sureButton.layer.borderColor = HexRGB(0x08f4e6).CGColor;
  180. [_sureButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  181. }
  182. return _sureButton;
  183. }
  184. /*
  185. // Only override drawRect: if you perform custom drawing.
  186. // An empty implementation adversely affects performance during animation.
  187. - (void)drawRect:(CGRect)rect {
  188. // Drawing code
  189. }
  190. */
  191. @end