LBInputAlertView.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // LBInputAlertView.m
  3. // LoveBaby_Parents
  4. //
  5. // Created by apple on 2018/5/23.
  6. // Copyright © 2018年 onecloud.ltd. All rights reserved.
  7. //
  8. #import "LBInputAlertView.h"
  9. #import "LBTextField.h"
  10. #import "BigBtn.h"
  11. #define LBINPUTALERTVIEWHEIGHT 219
  12. #define BtnHeight 32.0
  13. @interface LBInputAlertView ()
  14. /** 背景 */
  15. @property (nonatomic, strong) UIView *bgView;
  16. /** 白色弹窗view */
  17. @property (nonatomic, strong) UIView *containerView;
  18. /** 标题 */
  19. @property (nonatomic, strong) UILabel *titleLabel;
  20. /** 横线 */
  21. @property (nonatomic, strong) UIView *hLineView;
  22. /** 竖线 */
  23. @property (nonatomic, strong) UIView *vLineView;
  24. /** 取消 */
  25. @property (nonatomic, strong) BigBtn *cancelBtn;
  26. /** 确认 */
  27. @property (nonatomic, strong) BigBtn *confirmBtn;
  28. @end
  29. @implementation LBInputAlertView
  30. - (instancetype)init
  31. {
  32. if (self = [super init])
  33. {
  34. [self setupUI];
  35. }
  36. return self;
  37. }
  38. - (void)setupUI///<设置UI
  39. {
  40. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  41. [keyWindow addSubview:self.bgView];
  42. [self.bgView mas_makeConstraints:^(MASConstraintMaker *make)
  43. {
  44. make.top.left.bottom.right.equalTo(keyWindow);
  45. }];
  46. [keyWindow addSubview:self.containerView];
  47. [self.containerView mas_makeConstraints:^(MASConstraintMaker *make)
  48. {
  49. make.width.mas_equalTo(SCREENWIDTH * 0.76);
  50. make.centerY.equalTo(keyWindow).with.offset(-100.0);
  51. make.centerX.equalTo(keyWindow);
  52. make.height.mas_equalTo(LBINPUTALERTVIEWHEIGHT);
  53. }];
  54. [self.containerView addSubview:self.bgImgView];
  55. [self.bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
  56. make.edges.equalTo(self.containerView);
  57. }];
  58. //标题
  59. [self.containerView addSubview:self.titleLabel];
  60. [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make)
  61. {
  62. make.left.equalTo(self.containerView).with.offset(29.5);
  63. make.right.equalTo(self.containerView).with.offset(-27.5);
  64. make.top.equalTo(self.containerView).with.offset(28);
  65. }];
  66. //输入框
  67. [self.containerView addSubview:self.inputField];
  68. [self.inputField mas_makeConstraints:^(MASConstraintMaker *make)
  69. {
  70. make.left.equalTo(self.containerView).with.offset(22);
  71. make.right.equalTo(self.containerView).with.offset(-22);
  72. make.top.equalTo(self.titleLabel.mas_bottom).with.offset(20);
  73. make.height.mas_equalTo(40);
  74. }];
  75. //横线 (根据新UI 已经隐藏, 有需要放出)
  76. [self.containerView addSubview:self.hLineView];
  77. [self.hLineView mas_makeConstraints:^(MASConstraintMaker *make)
  78. {
  79. make.height.mas_equalTo(0.5);
  80. make.left.right.equalTo(self.containerView);
  81. make.bottom.equalTo(self.containerView).with.offset(-60);
  82. }];
  83. //竖线 (根据新UI 已经隐藏, 有需要放出)
  84. [self.containerView addSubview:self.vLineView];
  85. [self.vLineView mas_makeConstraints:^(MASConstraintMaker *make)
  86. {
  87. make.width.mas_equalTo(0.5);
  88. make.top.equalTo(self.hLineView.mas_bottom);
  89. make.bottom.equalTo(self.containerView);
  90. make.centerX.equalTo(self.containerView);
  91. }];
  92. self.hLineView.hidden = YES;
  93. self.vLineView.hidden = YES;
  94. //取消按钮
  95. [self.containerView addSubview:self.cancelBtn];
  96. [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make)
  97. {
  98. make.top.equalTo(self.hLineView.mas_bottom);
  99. make.left.equalTo(self.containerView).offset(22.0f);
  100. make.height.mas_equalTo(BtnHeight);
  101. }];
  102. //确定Button
  103. [self.containerView addSubview:self.confirmBtn];
  104. [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make)
  105. {
  106. make.height.equalTo(self.cancelBtn);
  107. make.width.equalTo(self.cancelBtn);
  108. make.top.equalTo(self.cancelBtn);
  109. make.left.equalTo(self.cancelBtn.mas_right).offset(20);
  110. make.right.mas_equalTo(-22);
  111. }];
  112. // CGFloat btnWidth = (SCREENWIDTH * 0.76 - 22.0 * 2.0 - 20.0) / 2.0;
  113. // NSArray *colorArr = @[[MOTools colorWithHexString:@"#FF62EE" alpha:1.0],[MOTools colorWithHexString:@"#9923FF" alpha:1.0]];
  114. // UIImage *image = [MOTools createGradientRectImageWithBounds:CGRectMake(0, 0, btnWidth, BtnHeight) Colors:colorArr GradientType:0];
  115. // [self.confirmBtn setBackgroundImage:image forState:UIControlStateNormal];
  116. }
  117. #pragma mark - 懒加载
  118. - (UIView *)bgView
  119. {
  120. if (!_bgView)
  121. {
  122. _bgView = [[UIView alloc] init];
  123. _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.6];
  124. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
  125. [_bgView addGestureRecognizer:tap];
  126. }
  127. return _bgView;
  128. }
  129. - (UIView *)containerView
  130. {
  131. if (!_containerView)
  132. {
  133. _containerView = [[UIView alloc] init];
  134. _containerView.backgroundColor = [MOTools colorWithHexString:@"#F8F9FC" alpha:1.0];
  135. _containerView.layer.masksToBounds = YES;
  136. _containerView.layer.cornerRadius = 16.0;
  137. }
  138. return _containerView;
  139. }
  140. - (UILabel *)titleLabel
  141. {
  142. if (!_titleLabel)
  143. {
  144. _titleLabel = [[UILabel alloc] init];
  145. _titleLabel.textColor = [MOTools colorWithHexString:@"#737373" alpha:1.0];;
  146. _titleLabel.font = [MOTextTools getTheFontWithSize:20.0 AndFontName:kNormalContentFontStr];
  147. _titleLabel.textAlignment = NSTextAlignmentCenter;
  148. _titleLabel.numberOfLines = 0;
  149. }
  150. return _titleLabel;
  151. }
  152. - (UITextField *)inputField
  153. {
  154. if (!_inputField)
  155. {
  156. _inputField = [[LBTextField alloc] init];
  157. _inputField.textColor = [MOTools colorWithHexString:@"#282828" alpha:1.0];
  158. _inputField.font = [MOTextTools getTheFontWithSize:16.0 AndFontName:kNormalContentFontStr];
  159. _inputField.backgroundColor = [UIColor clearColor];
  160. _inputField.layer.masksToBounds = YES;
  161. _inputField.layer.cornerRadius = 12.0;
  162. _inputField.layer.borderColor = [MOTools colorWithHexString:@"#ABABAB" alpha:1.0].CGColor;
  163. _inputField.layer.borderWidth = 0.5;
  164. }
  165. return _inputField;
  166. }
  167. - (UIView *)hLineView
  168. {
  169. if (!_hLineView)
  170. {
  171. _hLineView = [[UIView alloc] init];
  172. _hLineView.backgroundColor = [MOTools colorWithHexString:@"#EEEEEE" alpha:1.0];
  173. _hLineView.hidden = YES;
  174. }
  175. return _hLineView;
  176. }
  177. - (UIView *)vLineView
  178. {
  179. if (!_vLineView)
  180. {
  181. _vLineView = [[UIView alloc] init];
  182. _vLineView.backgroundColor = [MOTools colorWithHexString:@"#EEEEEE" alpha:1.0];
  183. _vLineView.hidden = YES;
  184. }
  185. return _vLineView;
  186. }
  187. - (BigBtn *)cancelBtn
  188. {
  189. if (!_cancelBtn)
  190. {
  191. _cancelBtn = [[BigBtn alloc] init];
  192. _cancelBtn.titleLabel.font = [MOTextTools getTheFontWithSize:20.0 AndFontName:kNormalContentFontStr];
  193. _cancelBtn.layer.borderWidth = 1.0f;
  194. UIColor *baseColor = [MOTools colorWithHexString:@"#FFFFFF" alpha:1.0];
  195. _cancelBtn.layer.borderColor = baseColor.CGColor;
  196. _cancelBtn.layer.masksToBounds = YES;
  197. _cancelBtn.layer.cornerRadius = BtnHeight / 2.0;
  198. [_cancelBtn setTitle:NSLocalString(@"mimo_Cancel") forState:UIControlStateNormal];
  199. [_cancelBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
  200. [_cancelBtn setTitleColor:baseColor forState:UIControlStateNormal];
  201. _cancelBtn.backgroundColor = [MOTools colorWithHexString:@"#B0B0B0" alpha:1.0];
  202. }
  203. return _cancelBtn;
  204. }
  205. - (BigBtn *)confirmBtn
  206. {
  207. if (!_confirmBtn)
  208. {
  209. _confirmBtn = [[BigBtn alloc] init];
  210. _confirmBtn.titleLabel.font = [MOTextTools getTheFontWithSize:20.0 AndFontName:kNormalContentFontStr];
  211. [_confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
  212. [_confirmBtn setTitle:NSLocalString(@"mimo_TipConfirm") forState:UIControlStateNormal];
  213. [_confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  214. _confirmBtn.layer.masksToBounds = YES;
  215. _confirmBtn.layer.cornerRadius = BtnHeight / 2.0;
  216. _confirmBtn.backgroundColor = kBaseBtnBgColor;
  217. }
  218. return _confirmBtn;
  219. }
  220. #pragma mark - my method
  221. - (void)setPlaceholder:(NSString *)placeholder
  222. {
  223. _placeholder = placeholder;
  224. self.inputField.placeholder = placeholder;
  225. }
  226. - (void)setTitleText:(NSString *)titleText//设置标题
  227. {
  228. _titleText = titleText;
  229. self.titleLabel.text = titleText;
  230. }
  231. - (void)setLeftBtnTitle:(NSString *)leftBtnTitle//设置左边按钮文字
  232. {
  233. [self.cancelBtn setTitle:leftBtnTitle forState:UIControlStateNormal];
  234. }
  235. - (void)setRightBtnTitle:(NSString *)rightBtnTitle//设置右边按钮文字
  236. {
  237. [self.confirmBtn setTitle:rightBtnTitle forState:UIControlStateNormal];
  238. }
  239. - (void)setTitleColor:(UIColor *)titleColor//设置标题颜色
  240. {
  241. self.titleLabel.textColor = titleColor;
  242. }
  243. - (void)setLeftBtnColor:(UIColor *)leftBtnColor//设置左边按钮颜色
  244. {
  245. [self.cancelBtn setTitleColor:leftBtnColor forState:UIControlStateNormal];
  246. }
  247. - (void)setRightBtnColor:(UIColor *)rightBtnColor//设置右边按钮颜色
  248. {
  249. [self.confirmBtn setTitleColor:rightBtnColor forState:UIControlStateNormal];
  250. }
  251. - (void)setShowAccessoryView:(BOOL)showAccessoryView
  252. {
  253. _showAccessoryView = showAccessoryView;
  254. if (showAccessoryView)
  255. {
  256. // UIButton *scanBtn = [[UIButton alloc] init];
  257. // [scanBtn setImage:[UIImage imageNamed:@"saoma"] forState:UIControlStateNormal];
  258. // scanBtn.frame = CGRectMake(0, 0, 40, 22);
  259. // self.inputField.rightView = scanBtn;
  260. // self.inputField.rightViewMode = UITextFieldViewModeAlways;
  261. // [scanBtn addTarget:self action:@selector(scanBtnClick) forControlEvents:UIControlEventTouchUpInside];
  262. }
  263. }
  264. - (void)scanBtnClick
  265. {
  266. [self.inputField resignFirstResponder];
  267. if (self.accessoryBlock)
  268. {
  269. self.accessoryBlock();
  270. }
  271. }
  272. - (void)cancelBtnClick//点击取消
  273. {
  274. [self dismiss];
  275. }
  276. - (void)confirmBtnClick//点击确认
  277. {
  278. [self.inputField resignFirstResponder];
  279. if (![self.inputField.text isEmpty])//有输入才dismiss
  280. {
  281. [self dismiss];
  282. }
  283. if (self.confirmBlock)
  284. {
  285. self.confirmBlock(self.inputField.text);
  286. }
  287. }
  288. - (void)show//界面显示动画
  289. {
  290. UIWindow *keyWindow = [[UIApplication sharedApplication] delegate].window;
  291. [keyWindow addSubview:self];
  292. //动画效果
  293. self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  294. self.containerView.alpha = 0;
  295. [UIView animateWithDuration:0.2 animations:^
  296. {
  297. self.containerView.transform = CGAffineTransformMakeScale(1.0, 1.0);
  298. self.containerView.alpha = 1;
  299. } completion:^(BOOL finished)
  300. {
  301. [self.inputField becomeFirstResponder];
  302. }];
  303. }
  304. - (void)dismiss//取消掉键盘
  305. {
  306. [self.inputField resignFirstResponder];
  307. [UIView animateWithDuration:0.2 animations:^
  308. {
  309. self.containerView.transform = CGAffineTransformMakeScale(1.3, 1.3);
  310. self.containerView.alpha = 0;
  311. } completion:^(BOOL finished)
  312. {
  313. if (finished)
  314. {
  315. [self.bgView removeFromSuperview];
  316. [self.containerView removeFromSuperview];
  317. [self removeFromSuperview];
  318. }
  319. }];
  320. }
  321. - (void)setNeedHide:(BOOL)needHide
  322. {
  323. if (needHide)
  324. {
  325. self.bgView.hidden = YES;
  326. self.containerView.hidden = YES;
  327. self.hidden = YES;
  328. }
  329. else
  330. {
  331. self.bgView.hidden = NO;
  332. self.containerView.hidden = NO;
  333. self.hidden = NO;
  334. }
  335. }
  336. - (UIImageView *)bgImgView{
  337. if(!_bgImgView){
  338. _bgImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon_alert_bg"]];
  339. _bgImgView.contentMode = UIViewContentModeScaleToFill;
  340. _bgImgView.backgroundColor = [UIColor clearColor];
  341. _bgImgView.hidden = YES;
  342. }
  343. return _bgImgView;
  344. }
  345. @end