CustomAlertView.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. //
  2. // CustomAlertView.m
  3. // Haishenghai-master
  4. //
  5. // Created by 刘云鸽 on 2019/4/8.
  6. // Copyright © 2019 Haishenghai intelligence network technology. All rights reserved.
  7. //
  8. #import "CustomAlertView.h"
  9. #import "MBProgressHUD.h"
  10. #define ScreenWidth [[UIScreen mainScreen] bounds].size.width
  11. #define ScreenHeight [[UIScreen mainScreen] bounds].size.height
  12. #define OnePixel (1./[UIScreen mainScreen].scale)
  13. #define animateTime 0.35f
  14. #define UIColorFromHEX(hexValue, alphaValue) \
  15. [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 \
  16. green:((float)((hexValue & 0x00FF00) >> 8))/255.0 \
  17. blue:((float)(hexValue & 0x0000FF))/255.0 \
  18. alpha:alphaValue]
  19. @interface CustomAlertView ()
  20. @property (nonatomic, assign) BOOL notifiKeyboardHide;
  21. @property (nonatomic, strong) UITextField * userTextField; //输入框
  22. @property (nonatomic, strong) UITextField * passwordTextField; //输入框
  23. @property (nonatomic, strong) UIView * alertBackgroundView;
  24. @property (nonatomic, strong) UIView * operateView; //操作视图
  25. @property (nonatomic, strong) UIButton * reloadImageBtn;
  26. @property (nonatomic, copy) ClickBlock confirmBlock;
  27. @property (nonatomic, copy) ReloadBlock reloadBlock;
  28. @end
  29. @implementation CustomAlertView
  30. + (CustomAlertView *)sharedAlertView
  31. {
  32. static dispatch_once_t once;
  33. static CustomAlertView * _alertView = nil;
  34. dispatch_once(&once, ^{
  35. if (_alertView == nil) {
  36. _alertView = [[self alloc] init];
  37. }
  38. });
  39. return _alertView;
  40. }
  41. #pragma mark - 创建UI
  42. - (void)showAlertViewWithConfirmAction:(ClickBlock)confirmBlock andReloadAction:(ReloadBlock)refreshBlcok
  43. {
  44. _notifiKeyboardHide = NO;
  45. [[NSNotificationCenter defaultCenter] addObserver:self
  46. selector:@selector(keyboardWillShow:)
  47. name:UIKeyboardWillShowNotification
  48. object:nil];
  49. /**
  50. * 背景视图
  51. */
  52. _alertBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
  53. _alertBackgroundView.backgroundColor = UIColorFromHEX(0x000000, 0.7);
  54. [[UIApplication sharedApplication].keyWindow addSubview:_alertBackgroundView];
  55. _alertBackgroundView.alpha = 0;
  56. [UIView animateWithDuration:animateTime animations:^{
  57. _alertBackgroundView.alpha = 1;
  58. }];
  59. /**
  60. * 操作区背景
  61. */
  62. _operateView = [[UIView alloc] init];
  63. _operateView.center = CGPointMake(ScreenWidth/2., ScreenHeight/2.);
  64. _operateView.bounds = CGRectMake(0, 0, ScreenWidth - 32, 208);
  65. _operateView.backgroundColor = [UIColor whiteColor];
  66. _operateView.layer.cornerRadius = 6;
  67. _operateView.clipsToBounds = YES;
  68. [_alertBackgroundView addSubview:_operateView];
  69. [self shakeToShow:_operateView];
  70. /**
  71. * 按钮
  72. */
  73. UIButton * cancelBtn = [self createButtonWithFrame:CGRectMake(0, CGRectGetHeight(_operateView.frame) - 48, _operateView.frame.size.width/2., 48) title:@"取消" andAction:@selector(removeAlertView)];
  74. [cancelBtn setBackgroundImage:[self imageWithColor:UIColorFromHEX(0xffffff, 1) andSize:cancelBtn.bounds.size] forState:UIControlStateNormal];
  75. self.confirmBlock = confirmBlock;
  76. UIButton * confirmBtn = [self createButtonWithFrame:CGRectMake(_operateView.frame.size.width/2., CGRectGetHeight(_operateView.frame) - 48, _operateView.frame.size.width/2., 48) title:@"确认" andAction:@selector(clickAction:)];
  77. [confirmBtn setTitleColor:[UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal];
  78. [confirmBtn setBackgroundImage:[self imageWithColor:UIColorFromHEX(0xffffff, 1) andSize:cancelBtn.bounds.size] forState:UIControlStateNormal];
  79. /**
  80. * 分割线
  81. */
  82. UILabel * horLine = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_operateView.frame) - 48 - OnePixel, _operateView.frame.size.width, OnePixel)];
  83. horLine.backgroundColor = UIColorFromHEX(0xcccccc, 1);
  84. [_operateView addSubview:horLine];
  85. UILabel * verLine = [[UILabel alloc] initWithFrame:CGRectMake(_operateView.frame.size.width/2. - OnePixel/2., CGRectGetHeight(_operateView.frame) - 48 - OnePixel, OnePixel, 48)];
  86. verLine.backgroundColor = UIColorFromHEX(0xcccccc, 1);
  87. [_operateView addSubview:verLine];
  88. /**
  89. *标题内容
  90. */
  91. UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_operateView.frame.size.width/2-60, 10,120 , 30)];
  92. titleLabel.text = @"添加联系人";
  93. titleLabel.font = [UIFont systemFontOfSize:18];
  94. titleLabel.textAlignment = NSTextAlignmentCenter;
  95. [_operateView addSubview:titleLabel];
  96. /**
  97. *姓名
  98. */
  99. UILabel *userLabel= [[UILabel alloc]initWithFrame:CGRectMake(20, _operateView.frame.size.height/2-50, 60, 44)];
  100. userLabel.text =@"姓名:";
  101. userLabel.font = [UIFont systemFontOfSize:18];
  102. [_operateView addSubview:userLabel];
  103. /**
  104. *号码
  105. */
  106. UILabel *passLabel= [[UILabel alloc]initWithFrame:CGRectMake(20, _operateView.frame.size.height/2+3, 60, 44)];
  107. passLabel.text =@"号码:";
  108. passLabel.font = [UIFont systemFontOfSize:18];
  109. [_operateView addSubview:passLabel];
  110. /**
  111. * 姓名输入框
  112. */
  113. _userTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, _operateView.frame.size.height/2-50, _operateView.frame.size.width-100, 44)];
  114. _userTextField.delegate = self;
  115. _userTextField.keyboardType = UIKeyboardTypeDefault;
  116. _userTextField.returnKeyType = UIReturnKeyDone;
  117. _userTextField.font = [UIFont systemFontOfSize:16];
  118. _userTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入姓名" attributes:@{NSForegroundColorAttributeName:UIColorFromHEX(0xcccccc, 1), NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  119. _userTextField.textColor = UIColorFromHEX(0x333333, 1);
  120. _userTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  121. [_operateView addSubview:_userTextField];
  122. UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(80, _operateView.frame.size.height/2-6, _operateView.frame.size.width-100, 0.5)];
  123. line1.backgroundColor = UIColorFromHEX(0xcccccc, 1);
  124. [_operateView addSubview:line1];
  125. /**
  126. * 密码输入框
  127. */
  128. _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, _operateView.frame.size.height/2+3, _operateView.frame.size.width-100, 44)];
  129. _passwordTextField.delegate = self;
  130. _passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
  131. _passwordTextField.returnKeyType = UIReturnKeyDone;
  132. _passwordTextField.font = [UIFont systemFontOfSize:16];
  133. _passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入号码" attributes:@{NSForegroundColorAttributeName:UIColorFromHEX(0xcccccc, 1), NSFontAttributeName:[UIFont systemFontOfSize:16]}];
  134. _passwordTextField.textColor = UIColorFromHEX(0x333333, 1);
  135. _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
  136. [_operateView addSubview:_passwordTextField];
  137. UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(80, _operateView.frame.size.height/2+47, _operateView.frame.size.width-100, 0.5)];
  138. line2.backgroundColor = UIColorFromHEX(0xcccccc, 1);
  139. [_operateView addSubview:line2];
  140. }
  141. #pragma mark - 移除视图
  142. - (void)removeAlertView
  143. {
  144. if ([_userTextField isFirstResponder]) {
  145. [_userTextField resignFirstResponder];
  146. }
  147. if ([_passwordTextField isFirstResponder]) {
  148. [_passwordTextField resignFirstResponder];
  149. }
  150. //退出
  151. [UIView animateWithDuration:animateTime animations:^{
  152. _alertBackgroundView.alpha = 0;
  153. } completion:^(BOOL finished) {
  154. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
  155. if (_notifiKeyboardHide) {
  156. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
  157. }
  158. [self.alertBackgroundView removeFromSuperview];
  159. self.alertBackgroundView = nil;
  160. self.operateView = nil;
  161. }];
  162. }
  163. #pragma mark - 创建按钮
  164. - (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title andAction:(SEL)action
  165. {
  166. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  167. btn.frame = frame;
  168. [btn setTitleColor:UIColorFromHEX(0x333333, 1) forState:UIControlStateNormal];
  169. [btn setTitle:title forState:UIControlStateNormal];
  170. btn.titleLabel.font = [UIFont boldSystemFontOfSize:18];
  171. [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
  172. [_operateView addSubview:btn];
  173. return btn;
  174. }
  175. - (void)clickAction:(UIButton *)sender
  176. {
  177. if (self.confirmBlock) {
  178. if (_userTextField.text.length ==0||_passwordTextField.text.length ==0) {
  179. [self removeAlertView];
  180. UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  181. UIViewController *topVC = appRootVC;
  182. if (topVC.presentedViewController) {
  183. topVC = topVC.presentedViewController;
  184. }
  185. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  186. hud.mode = MBProgressHUDModeText;
  187. hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  188. hud.bezelView.color = [UIColor grayColor];
  189. hud.label.text = @"姓名或手机号不能为空";
  190. hud.label.textColor = [UIColor whiteColor];
  191. hud.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变
  192. [hud hideAnimated:YES afterDelay:1];
  193. return;
  194. }else if(_passwordTextField.text.length !=11){
  195. [self removeAlertView];
  196. UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
  197. UIViewController *topVC = appRootVC;
  198. if (topVC.presentedViewController) {
  199. topVC = topVC.presentedViewController;
  200. }
  201. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:topVC.view animated:YES];
  202. hud.mode = MBProgressHUDModeText;
  203. hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
  204. hud.bezelView.color = [UIColor grayColor];
  205. hud.label.text = @"请输入正确的手机号";
  206. hud.label.textColor = [UIColor whiteColor];
  207. hud.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变
  208. [hud hideAnimated:YES afterDelay:1];
  209. }else{
  210. NSArray *titlearray = @[_userTextField.text,_passwordTextField.text];
  211. self.confirmBlock(titlearray);
  212. [self removeAlertView];
  213. }
  214. }
  215. }
  216. - (void)reloadImageAction:(UIButton *)sender
  217. {
  218. [self removeAlertView];
  219. }
  220. #pragma mark - 监听键盘弹起,操作框动画
  221. ///键盘弹起,页面动画,监听
  222. - (void)keyboardWillShow:(NSNotification *)notification
  223. {
  224. // 键盘的frame
  225. CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
  226. CGFloat keyboardHeight = keyboardRect.size.height;
  227. CGFloat keyboardOriginY = ScreenHeight - keyboardHeight;
  228. CGFloat operateMaxY = ScreenHeight/2. + _operateView.bounds.size.height/2. + 16;
  229. // DLog(@"--- %lf %lf", keyboardOriginY, operateMaxY);
  230. if (operateMaxY >= keyboardOriginY) {
  231. [UIView animateWithDuration:0.25 animations:^{
  232. CGRect rect = _operateView.frame;
  233. rect.origin.y = keyboardOriginY - rect.size.height - 16;
  234. _operateView.frame = rect;
  235. } completion:^(BOOL finished) {
  236. }];
  237. _notifiKeyboardHide = YES;
  238. [[NSNotificationCenter defaultCenter] addObserver:self
  239. selector:@selector(keyboardWillHide:)
  240. name:UIKeyboardWillHideNotification
  241. object:nil];
  242. }
  243. else {
  244. _notifiKeyboardHide = NO;
  245. }
  246. }
  247. ///键盘收起,页面动画,监听
  248. - (void)keyboardWillHide:(NSNotification *)notification
  249. {
  250. [UIView animateWithDuration:0.25 animations:^{
  251. CGRect rect = _operateView.frame;
  252. rect.origin.y = (ScreenHeight - rect.size.height)/2.;
  253. _operateView.frame = rect;
  254. } completion:^(BOOL finished) {
  255. }];
  256. }
  257. #pragma mark - 输入框代理
  258. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  259. {
  260. [textField resignFirstResponder];
  261. return YES;
  262. }
  263. #pragma mark - 颜色转换为图片
  264. - (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)aSize
  265. {
  266. CGRect rect = CGRectMake(0.0f, 0.0f, aSize.width, aSize.height);
  267. UIGraphicsBeginImageContext(rect.size);
  268. CGContextRef context = UIGraphicsGetCurrentContext();
  269. CGContextSetFillColorWithColor(context, [color CGColor]);
  270. CGContextFillRect(context, rect);
  271. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  272. UIGraphicsEndImageContext();
  273. return image;
  274. }
  275. #pragma mark - 弹性震颤动画
  276. - (void)shakeToShow:(UIView *)aView
  277. {
  278. CAKeyframeAnimation * popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
  279. popAnimation.duration = 0.35;
  280. popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)],
  281. [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05f, 1.05f, 1.0f)],
  282. // [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)],
  283. [NSValue valueWithCATransform3D:CATransform3DIdentity]];
  284. popAnimation.keyTimes = @[@0.0f, @0.5f, /*@0.75f,*/ @0.8f];
  285. popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
  286. // [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut],
  287. [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  288. [aView.layer addAnimation:popAnimation forKey:nil];
  289. }
  290. @end