EMAlertController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // EMAlertController.m
  3. // ChatDemo-UI3.0
  4. //
  5. // Created by XieYajie on 2018/12/24.
  6. // Copyright © 2018 XieYajie. All rights reserved.
  7. //
  8. #import "EMAlertController.h"
  9. @interface EMAlertController()
  10. @property (nonatomic, strong) UIView *mainView;
  11. @end
  12. @implementation EMAlertController
  13. - (instancetype)initWithStyle:(EMAlertViewStyle)aStyle
  14. message:(NSString *)aMessage
  15. {
  16. self = [super init];
  17. if (self) {
  18. [self _setupWithStyle:aStyle message:aMessage];
  19. }
  20. return self;
  21. }
  22. - (void)_setupWithStyle:(EMAlertViewStyle)aStyle
  23. message:(NSString *)aMessage
  24. {
  25. self.backgroundColor = [UIColor colorWithWhite:0.2 alpha:0.1];
  26. self.mainView = [[UIView alloc] init];
  27. self.mainView.backgroundColor = [UIColor whiteColor];
  28. self.mainView.layer.cornerRadius = 5.0;
  29. self.mainView.layer.shadowColor = [UIColor grayColor].CGColor;
  30. self.mainView.layer.shadowOffset = CGSizeMake(2, 5);
  31. self.mainView.layer.shadowOpacity = 0.5;
  32. [self addSubview:self.mainView];
  33. [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.equalTo(self).offset(-60);
  35. make.centerX.equalTo(self);
  36. make.left.greaterThanOrEqualTo(self).offset(30);
  37. }];
  38. UIView *bgView = [[UIView alloc] init];
  39. bgView.backgroundColor = [UIColor clearColor];
  40. bgView.clipsToBounds = YES;
  41. bgView.layer.cornerRadius = 5.0;
  42. [self.mainView addSubview:bgView];
  43. [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.edges.equalTo(self.mainView);
  45. }];
  46. UIView *line = [[UIView alloc] init];
  47. line.backgroundColor = [self _tagColorWithStyle:aStyle];
  48. [bgView addSubview:line];
  49. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.top.equalTo(bgView);
  51. make.bottom.equalTo(bgView);
  52. make.left.equalTo(bgView);
  53. make.width.equalTo(@3);
  54. }];
  55. UIImageView *tagView = [[UIImageView alloc] init];
  56. tagView.image = [self _tagImageWithStyle:aStyle];
  57. [bgView addSubview:tagView];
  58. [tagView mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.centerY.equalTo(bgView);
  60. make.left.equalTo(bgView).offset(15);
  61. }];
  62. UILabel *label = [[UILabel alloc] init];
  63. label.numberOfLines = 5;
  64. label.font = [UIFont systemFontOfSize:16];
  65. label.text = aMessage;
  66. [bgView addSubview:label];
  67. [label mas_makeConstraints:^(MASConstraintMaker *make) {
  68. make.left.equalTo(tagView.mas_right).offset(10);
  69. make.right.equalTo(bgView).offset(-15);
  70. make.top.equalTo(bgView).offset(12);
  71. make.bottom.equalTo(bgView).offset(-12);
  72. }];
  73. }
  74. - (UIColor *)_tagColorWithStyle:(EMAlertViewStyle)aStyle
  75. {
  76. UIColor *color = [UIColor blackColor];
  77. switch (aStyle) {
  78. case EMAlertViewStyleError:
  79. color = [UIColor colorWithRed:204 / 255.0 green:58 / 255.0 blue:35 / 255.0 alpha:1.0];
  80. break;
  81. case EMAlertViewStyleInfo:
  82. color = [UIColor colorWithRed:232 / 255.0 green:192 / 255.0 blue:64 / 255.0 alpha:1.0];
  83. break;
  84. case EMAlertViewStyleSuccess:
  85. color = [UIColor colorWithRed:35 / 255.0 green:158 / 255.0 blue:85 / 255.0 alpha:1.0];
  86. break;
  87. default:
  88. break;
  89. }
  90. return color;
  91. }
  92. - (UIImage *)_tagImageWithStyle:(EMAlertViewStyle)aStyle
  93. {
  94. NSString *imageName = @"alert_default";
  95. switch (aStyle) {
  96. case EMAlertViewStyleError:
  97. imageName = @"alert_error";
  98. break;
  99. case EMAlertViewStyleInfo:
  100. imageName = @"alert_info";
  101. break;
  102. case EMAlertViewStyleSuccess:
  103. imageName = @"alert_success";
  104. break;
  105. default:
  106. break;
  107. }
  108. return [UIImage imageNamed:imageName];
  109. }
  110. + (void)showAlertWithStyle:(EMAlertViewStyle)aStyle
  111. message:(NSString *)aMessage
  112. {
  113. EMAlertController *view = [[EMAlertController alloc] initWithStyle:aStyle message:aMessage];
  114. UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
  115. [keyWindow addSubview:view];
  116. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.edges.equalTo(keyWindow);
  118. }];
  119. [view layoutIfNeeded];
  120. [view setNeedsUpdateConstraints];
  121. [view.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
  122. make.top.equalTo(view).offset(50);
  123. }];
  124. [UIView animateWithDuration:0.3 animations:^{
  125. [view layoutIfNeeded];
  126. } completion:^(BOOL finished) {
  127. //
  128. }];
  129. dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC);
  130. dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
  131. [view layoutIfNeeded];
  132. [view setNeedsUpdateConstraints];
  133. [view.mainView mas_updateConstraints:^(MASConstraintMaker *make) {
  134. make.top.equalTo(view).offset(-60);
  135. }];
  136. [UIView animateWithDuration:0.3 animations:^{
  137. [view layoutIfNeeded];
  138. } completion:^(BOOL finished) {
  139. [view removeFromSuperview];
  140. }];
  141. });
  142. }
  143. + (void)showErrorAlert:(NSString *)aMessage
  144. {
  145. [EMAlertController showAlertWithStyle:EMAlertViewStyleError message:aMessage];
  146. }
  147. + (void)showSuccessAlert:(NSString *)aMessage
  148. {
  149. [EMAlertController showAlertWithStyle:EMAlertViewStyleSuccess message:aMessage];
  150. }
  151. + (void)showInfoAlert:(NSString *)aMessage
  152. {
  153. [EMAlertController showAlertWithStyle:EMAlertViewStyleInfo message:aMessage];
  154. }
  155. @end