MOTitleNormalAlertView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // MOTitleNormalAlertView.m
  3. // MiMoLive
  4. //
  5. // Created by SuperC on 2023/11/5.
  6. //
  7. #define LBINPUTALERTVIEWHEIGHT 219.0
  8. #define BtnBottomSpacing 24.0
  9. #define BtnLeftSpacing 20.0
  10. #define BtnHeight 36.0
  11. #define LeftAndRightSpacing 15.0
  12. #import "MOTitleNormalAlertView.h"
  13. @interface MOTitleNormalAlertView ()
  14. /** 背景 */
  15. @property (nonatomic, strong) UIView *bgView;
  16. @end
  17. @implementation MOTitleNormalAlertView
  18. - (instancetype)init
  19. {
  20. if (self = [super init])
  21. {
  22. [self setupUI];
  23. }
  24. return self;
  25. }
  26. - (void)setupUI{
  27. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  28. [keyWindow addSubview:self.bgView];
  29. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make)
  30. {
  31. make.top.left.bottom.right.equalTo(keyWindow);
  32. }];
  33. [keyWindow addSubview:self.containerView];
  34. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make)
  35. {
  36. make.width.mas_equalTo(SCREENWIDTH * 0.76);
  37. make.centerY.equalTo(keyWindow).with.offset(-100.0);
  38. make.centerX.equalTo(keyWindow);
  39. make.height.mas_equalTo(LBINPUTALERTVIEWHEIGHT);
  40. }];
  41. self.twoBtnWidth = SCREENWIDTH * 0.76 / 2.0;
  42. [self.containerView addSubview:self.bgImgView];
  43. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  44. make.edges.equalTo(self.containerView);
  45. }];
  46. //标题
  47. [self.containerView addSubview:self.titleLabel];
  48. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make)
  49. {
  50. make.left.equalTo(self.containerView).with.offset(16.0);
  51. make.right.equalTo(self.containerView).with.offset(-16.0);
  52. make.top.equalTo(self.containerView).with.offset(22);
  53. }];
  54. [self.containerView addSubview:self.closeBtn];
  55. [self.closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.width.height.equalTo(@14.0);
  57. make.top.equalTo(self.containerView).offset(12.0);
  58. make.right.equalTo(self.containerView).offset(-10.0);
  59. }];
  60. self.closeBtn.hidden = YES;
  61. [self.containerView addSubview:self.subTitleLabel];
  62. [self.subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  63. make.left.equalTo(self.containerView).with.offset(16.0);
  64. make.right.equalTo(self.containerView).with.offset(-16.0);
  65. make.top.equalTo(self.titleLabel.mas_bottom).with.offset(12);
  66. }];
  67. //取消按钮
  68. [self.containerView addSubview:self.cancelBtn];
  69. [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make)
  70. {
  71. make.bottom.equalTo(self.containerView).offset(-BtnBottomSpacing);
  72. make.left.equalTo(self.containerView).offset(BtnLeftSpacing);
  73. make.height.mas_equalTo(BtnHeight);
  74. }];
  75. //确定Button
  76. [self.containerView addSubview:self.confirmBtn];
  77. [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make)
  78. {
  79. make.height.equalTo(self.cancelBtn);
  80. make.width.equalTo(self.cancelBtn);
  81. make.top.equalTo(self.cancelBtn);
  82. make.left.equalTo(self.cancelBtn.mas_right).offset(LeftAndRightSpacing);
  83. make.right.mas_equalTo(-BtnLeftSpacing);
  84. }];
  85. }
  86. - (void)calculateHeight{
  87. //主标题高度
  88. CGFloat titleHeight = [MOTools calculateRowHeight:self.titleLabel.text font:self.titleLabel.font andWidth:(SCREENWIDTH * 0.76 - 16.0 * 2)];
  89. CGFloat subTitleHeight = [MOTools calculateRowHeight:self.subTitleLabel.text font:self.subTitleLabel.font andWidth:(SCREENWIDTH * 0.76 - 16.0 * 2)];
  90. CGFloat contentViewHeight = 24.0 + titleHeight + 10.0 + subTitleHeight + 20.0 + BtnHeight + BtnBottomSpacing;
  91. [self.containerView mas_updateConstraints:^(MASConstraintMaker *make)
  92. {
  93. make.height.mas_equalTo(contentViewHeight);
  94. }];
  95. }
  96. #pragma mark - my method
  97. - (void)setIsSingleBtn:(BOOL)isSingleBtn
  98. {
  99. _isSingleBtn = isSingleBtn;
  100. //单个按钮
  101. if (isSingleBtn)
  102. {
  103. self.confirmBtn.layer.cornerRadius = BtnHeight / 2.0;
  104. [self.confirmBtn mas_remakeConstraints:^(MASConstraintMaker *make)
  105. {
  106. make.bottom.equalTo(self.containerView).offset(-BtnBottomSpacing);
  107. make.left.equalTo(self.containerView).offset(45);
  108. make.right.equalTo(self.containerView).offset(-45);
  109. make.height.mas_equalTo(BtnHeight);
  110. }];
  111. [self.cancelBtn removeFromSuperview];
  112. }
  113. }
  114. - (void)setIsShowCloseBtn:(BOOL)isShowCloseBtn{
  115. _isShowCloseBtn = isShowCloseBtn;
  116. if(isShowCloseBtn){
  117. self.closeBtn.hidden = NO;
  118. }
  119. }
  120. - (void)setIsVipTip:(BOOL)isVipTip{
  121. _isVipTip = isVipTip;
  122. if(isVipTip){
  123. [self.bgImgView setImage:[UIImage imageNamed:@"icon_vip_buy_tip_bg"]];
  124. self.containerView.backgroundColor = [UIColor clearColor];
  125. self.bgImgView.hidden = NO;
  126. self.titleLabel.textColor = [MOTools colorWithHexString:@"#D3B88D" alpha:1.0];
  127. [self.cancelBtn setBackgroundColor:[MOTools colorWithHexString:@"#B0B0B0" alpha:1.0]];
  128. [self.cancelBtn setTitleColor:[MOTools colorWithHexString:@"#FFFFFF" alpha:1.0] forState:UIControlStateNormal];
  129. [self.confirmBtn setBackgroundColor:[MOTools colorWithHexString:@"#E8C786" alpha:1.0]];
  130. [self.confirmBtn setTitleColor:[MOTools colorWithHexString:@"#582D1E" alpha:1.0] forState:UIControlStateNormal];
  131. }
  132. }
  133. - (void)show//界面显示动画
  134. {
  135. [self calculateHeight];
  136. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  137. [keyWindow addSubview:self];
  138. //动画效果
  139. self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  140. self.containerView.alpha = 0;
  141. [UIView animateWithDuration:0.2 animations:^
  142. {
  143. self.containerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  144. self.containerView.alpha = 1;
  145. } completion:^(BOOL finished)
  146. {
  147. }];
  148. }
  149. - (void)confirmBtnClick//点击确认
  150. {
  151. //点击确认
  152. if (self.confirmBlock)
  153. {
  154. self.confirmBlock();
  155. }
  156. [self dismiss];
  157. }
  158. - (void)dismiss//取消掉键盘
  159. {
  160. [UIView animateWithDuration:0.2 animations:^
  161. {
  162. self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  163. self.containerView.alpha = 0;
  164. } completion:^(BOOL finished)
  165. {
  166. if (finished)
  167. {
  168. [self.bgView removeFromSuperview];
  169. [self.containerView removeFromSuperview];
  170. [self removeFromSuperview];
  171. }
  172. }];
  173. self.dismissBlock ? self.dismissBlock() : nil;
  174. }
  175. #pragma mark - 懒加载
  176. - (UIView *)bgView
  177. {
  178. if (!_bgView)
  179. {
  180. _bgView = [[UIView alloc] init];
  181. _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  182. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgViewTapClick)];
  183. [_bgView addGestureRecognizer:tap];
  184. }
  185. return _bgView;
  186. }
  187. - (void)bgViewTapClick{
  188. if(self.isStrongBg){
  189. return;
  190. }
  191. [self dismiss];
  192. }
  193. - (UIView *)containerView
  194. {
  195. if (!_containerView)
  196. {
  197. _containerView = [[UIView alloc] init];
  198. _containerView.backgroundColor = [MOTools colorWithHexString:@"#FFFFFF"];
  199. _containerView.layer.masksToBounds = YES;
  200. _containerView.layer.cornerRadius = 16.0;
  201. }
  202. return _containerView;
  203. }
  204. - (UILabel *)titleLabel
  205. {
  206. if (!_titleLabel)
  207. {
  208. _titleLabel = [[UILabel alloc] init];
  209. _titleLabel.textColor = kBaseTextColor_1;
  210. _titleLabel.font = [MOTextTools mediumFont:16];
  211. _titleLabel.textAlignment = NSTextAlignmentCenter;
  212. _titleLabel.numberOfLines = 0;
  213. _titleLabel.text = @"";
  214. }
  215. return _titleLabel;
  216. }
  217. - (UILabel *)subTitleLabel
  218. {
  219. if (!_subTitleLabel)
  220. {
  221. _subTitleLabel = [[UILabel alloc] init];
  222. _subTitleLabel.textColor = kBaseTextColor_2;
  223. _subTitleLabel.textAlignment = NSTextAlignmentCenter;
  224. _subTitleLabel.font = [MOTextTools regularFont:13];
  225. _subTitleLabel.numberOfLines = 0;
  226. }
  227. return _subTitleLabel;
  228. }
  229. - (BigBtn *)cancelBtn
  230. {
  231. if (!_cancelBtn)
  232. {
  233. _cancelBtn = [[BigBtn alloc] init];
  234. _cancelBtn.titleLabel.font = [MOTextTools regularFont:16];
  235. _cancelBtn.layer.borderWidth = 1.0f;
  236. _cancelBtn.layer.borderColor = [MOTools colorWithHexString:@"#DADCE6"].CGColor;
  237. [_cancelBtn setBackgroundColor:[MOTools colorWithHexString:@"#FFFFFF" alpha:1.0]];
  238. _cancelBtn.layer.masksToBounds = YES;
  239. _cancelBtn.layer.cornerRadius = 12;
  240. [_cancelBtn setTitle:@"Cancel" forState:UIControlStateNormal];
  241. [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  242. [_cancelBtn setTitleColor:kBaseTextColor_1 forState:UIControlStateNormal];
  243. }
  244. return _cancelBtn;
  245. }
  246. - (void)cancelBtnClick{
  247. if (self.cancelBlock)
  248. {
  249. self.cancelBlock();
  250. }
  251. [self dismiss];
  252. }
  253. - (BigBtn *)confirmBtn
  254. {
  255. if (!_confirmBtn)
  256. {
  257. _confirmBtn = [[BigBtn alloc] init];
  258. _confirmBtn.titleLabel.font = [MOTextTools regularFont:16];
  259. [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  260. [_confirmBtn setTitle:@"Sure" forState:UIControlStateNormal];
  261. [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  262. _confirmBtn.layer.masksToBounds = YES;
  263. _confirmBtn.layer.cornerRadius = 12;
  264. NSArray *colorArr = @[kBaseColorLeft,kBaseColorRight];
  265. CGFloat btnWidht = ((SCREENWIDTH * 0.76) - BtnLeftSpacing * 2 - LeftAndRightSpacing) / 2;
  266. UIImage *image = [MOTools createGradientRectImageWithBounds:CGRectMake(0, 0,btnWidht, BtnHeight) Colors:colorArr GradientType:0];
  267. [_confirmBtn setBackgroundImage:image forState:UIControlStateNormal];
  268. }
  269. return _confirmBtn;
  270. }
  271. - (UIImageView *)bgImgView{
  272. if(!_bgImgView){
  273. _bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_alert_bg"]];
  274. _bgImgView.contentMode = UIViewContentModeScaleToFill;
  275. _bgImgView.backgroundColor = [UIColor clearColor];
  276. _bgImgView.hidden = YES;
  277. }
  278. return _bgImgView;
  279. }
  280. - (BigBtn *)closeBtn
  281. {
  282. if (!_closeBtn)
  283. {
  284. _closeBtn = [[BigBtn alloc] init];
  285. [_closeBtn setBackgroundImage:[UIImage imageNamed:@"icon_strong_close"] forState:UIControlStateNormal];
  286. [_closeBtn addTarget:self action:@selector(closeBtnClick) forControlEvents:UIControlEventTouchUpInside];
  287. }
  288. return _closeBtn;
  289. }
  290. - (void)closeBtnClick{
  291. [self dismiss];
  292. }
  293. @end