财神随手记账

WZNoticeAlertView.m 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // WZNoticeAlertView.m
  3. // WZNoticeAlertView
  4. //
  5. // Created by juru on 2018/1/27.
  6. // Copyright © 2018年 wangzhao. All rights reserved.
  7. //
  8. #import "Masonry.h"
  9. #import "WZNoticeAlertView.h"
  10. @interface WZNoticeAlertView ()
  11. @property(nonatomic, strong) UILabel *titleLabel;
  12. @property(nonatomic, strong) UIButton *removeBtn;
  13. @property(nonatomic, strong) UIImageView *noticeImage;
  14. @property(nonatomic, strong) UILabel *messageLabel;
  15. @property(nonatomic, strong) UIView *screenView;
  16. @end
  17. @implementation WZNoticeAlertView{
  18. UIButton *firsBtn;
  19. UIButton *nextBtn;
  20. }
  21. - (instancetype)initWithFrame:(CGRect)frame
  22. btnTitle:(NSString *)btnTitle
  23. nextBtnTitle:(NSString *)nextBtnTitle
  24. iconImageStr:(NSString *)iconImageStr
  25. message:(NSString *)message
  26. title:(NSString *)title
  27. removeImageStr:(NSString *)removeImageStr
  28. {
  29. self = [super initWithFrame:frame];
  30. if (self) {
  31. self.backgroundColor = [UIColor whiteColor];
  32. self.btnTitle = btnTitle;
  33. self.nextBtnTitle = nextBtnTitle;
  34. self.iconImageStr = iconImageStr;
  35. self.message = message;
  36. self.title = title;
  37. self.removeImageStr = removeImageStr;
  38. [self setUpViews];
  39. [self creatAutoLayout];
  40. }
  41. return self;
  42. }
  43. - (void)setUpViews{
  44. if (self.removeImageStr) {
  45. [self.removeBtn setBackgroundImage:[UIImage imageNamed:self.removeImageStr] forState:UIControlStateNormal];
  46. }
  47. [self addSubview:self.removeBtn];
  48. [self addSubview:self.titleLabel];
  49. self.titleLabel.text = self.title;
  50. [self addSubview:self.noticeImage];
  51. if (self.iconImageStr) {
  52. self.noticeImage.image = [UIImage imageNamed:self.iconImageStr];
  53. }
  54. [self addSubview:self.messageLabel];
  55. if (self.message) {
  56. self.messageLabel.text = self.message;
  57. }
  58. }
  59. - (void)creatAutoLayout{
  60. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  61. make.centerX.mas_equalTo(self);
  62. make.top.mas_equalTo(self.mas_top).offset(20);
  63. }];
  64. [self.removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.right.mas_equalTo(self.mas_right).offset(-20);
  66. make.centerY.mas_equalTo(self.titleLabel);
  67. }];
  68. [self.noticeImage mas_makeConstraints:^(MASConstraintMaker *make) {
  69. make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(40);
  70. make.centerX.mas_equalTo(self);
  71. }];
  72. [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.top.mas_equalTo(self.noticeImage.mas_bottom).offset(35);
  74. make.left.mas_equalTo(self.mas_left).offset(40);
  75. make.right.mas_equalTo(self.mas_right).offset(-40);
  76. }];
  77. [_removeBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
  78. firsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  79. [firsBtn setTitle:_btnTitle forState:UIControlStateNormal];
  80. [firsBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  81. [firsBtn setBackgroundImage:[UIImage imageNamed:@"btn_bg_default"] forState:UIControlStateNormal];
  82. [firsBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
  83. [self addSubview:firsBtn];
  84. [firsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  85. make.top.mas_equalTo(self.messageLabel.mas_bottom).offset(35);
  86. make.left.mas_equalTo(self.mas_left).offset(40);
  87. make.right.mas_equalTo(self.mas_right).offset(-40);
  88. }];
  89. if (self.nextBtnTitle) {
  90. nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  91. [nextBtn setTitle:_nextBtnTitle forState:UIControlStateNormal];
  92. [nextBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  93. [nextBtn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
  94. [self addSubview:nextBtn];
  95. [self.messageLabel mas_updateConstraints:^(MASConstraintMaker *make) {
  96. make.top.mas_equalTo(self.noticeImage.mas_bottom).offset(15);
  97. }];
  98. [firsBtn mas_updateConstraints:^(MASConstraintMaker *make) {
  99. make.top.mas_equalTo(self.messageLabel.mas_bottom).offset(15);
  100. make.left.mas_equalTo(self.mas_left).offset(40);
  101. make.right.mas_equalTo(self.mas_right).offset(-40);
  102. }];
  103. [nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  104. make.top.mas_equalTo(firsBtn.mas_bottom).offset(15);
  105. make.centerX.mas_equalTo(self);
  106. }];
  107. }
  108. }
  109. #pragma mark lazy
  110. - (UILabel *)titleLabel{
  111. if (_titleLabel == nil) {
  112. _titleLabel = [[UILabel alloc] init];
  113. _titleLabel.textColor = [UIColor blackColor];
  114. }
  115. return _titleLabel;
  116. }
  117. - (UIButton *)removeBtn{
  118. if (_removeBtn == nil) {
  119. _removeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  120. [_removeBtn setBackgroundImage:[UIImage imageNamed:@"gold_mine_icon"] forState:UIControlStateNormal];
  121. }
  122. return _removeBtn;
  123. }
  124. - (UIImageView *)noticeImage{
  125. if (_noticeImage == nil) {
  126. _noticeImage = [[UIImageView alloc] init];
  127. _noticeImage.image = [UIImage imageNamed:@""];
  128. }
  129. return _noticeImage;
  130. }
  131. - (UILabel *)messageLabel{
  132. if (_messageLabel == nil) {
  133. _messageLabel = [[UILabel alloc] init];
  134. _messageLabel.textColor = [UIColor lightGrayColor];
  135. _messageLabel.numberOfLines = 0;
  136. }
  137. return _messageLabel;
  138. }
  139. - (UIView *)screenView{
  140. if (_screenView == nil) {
  141. _screenView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
  142. _screenView.backgroundColor = [UIColor blackColor];
  143. _screenView.alpha = 1;
  144. }
  145. return _screenView;
  146. }
  147. - (void)show{
  148. [self.screenView addSubview:self];
  149. UIWindow *window = [UIApplication sharedApplication].keyWindow;
  150. [window addSubview:self.screenView];
  151. self.screenView .backgroundColor = [UIColor colorWithRed:0/255.0
  152. green:0/255.0
  153. blue:0/255.0
  154. alpha:0.5];
  155. self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 270)/2,[UIScreen mainScreen].bounds.size.height , 270, 400);
  156. [UIView animateWithDuration:0.2 animations:^{
  157. self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 270)/2,([UIScreen mainScreen].bounds.size.height - 400)/2, 270, 400);
  158. }];
  159. }
  160. - (void)remove{
  161. [UIView animateWithDuration:0.35 animations:^
  162. {
  163. self.frame = CGRectMake(([UIScreen mainScreen].bounds.size.width - 270)/2,[UIScreen mainScreen].bounds.size.height , 270, 400);
  164. } completion:^(BOOL finished)
  165. {
  166. [self.screenView removeFromSuperview];
  167. }];
  168. }
  169. - (void)click:(UIButton *)btn{
  170. if (btn == nextBtn) {
  171. if (self.tapCallback != nil) {
  172. self.tapCallback(1);
  173. }
  174. }else if (btn == firsBtn){
  175. if (self.tapCallback != nil) {
  176. self.tapCallback(0);
  177. }
  178. }else if (btn == self.removeBtn){
  179. if (self.close) {
  180. self.close();
  181. }
  182. }
  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