// // CustomAlertView.m // Haishenghai-master // // Created by 刘云鸽 on 2019/4/8. // Copyright © 2019 Haishenghai intelligence network technology. All rights reserved. // #import "CustomAlertView.h" #import "MBProgressHUD.h" #define ScreenWidth [[UIScreen mainScreen] bounds].size.width #define ScreenHeight [[UIScreen mainScreen] bounds].size.height #define OnePixel (1./[UIScreen mainScreen].scale) #define animateTime 0.35f #define UIColorFromHEX(hexValue, alphaValue) \ [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0 \ green:((float)((hexValue & 0x00FF00) >> 8))/255.0 \ blue:((float)(hexValue & 0x0000FF))/255.0 \ alpha:alphaValue] @interface CustomAlertView () @property (nonatomic, assign) BOOL notifiKeyboardHide; @property (nonatomic, strong) UITextField * userTextField; //输入框 @property (nonatomic, strong) UITextField * passwordTextField; //输入框 @property (nonatomic, strong) UIView * alertBackgroundView; @property (nonatomic, strong) UIView * operateView; //操作视图 @property (nonatomic, strong) UIButton * reloadImageBtn; @property (nonatomic, copy) ClickBlock confirmBlock; @property (nonatomic, copy) ReloadBlock reloadBlock; @end @implementation CustomAlertView + (CustomAlertView *)sharedAlertView { static dispatch_once_t once; static CustomAlertView * _alertView = nil; dispatch_once(&once, ^{ if (_alertView == nil) { _alertView = [[self alloc] init]; } }); return _alertView; } #pragma mark - 创建UI - (void)showAlertViewWithConfirmAction:(ClickBlock)confirmBlock andReloadAction:(ReloadBlock)refreshBlcok { _notifiKeyboardHide = NO; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; /** * 背景视图 */ _alertBackgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)]; _alertBackgroundView.backgroundColor = UIColorFromHEX(0x000000, 0.7); [[UIApplication sharedApplication].keyWindow addSubview:_alertBackgroundView]; _alertBackgroundView.alpha = 0; [UIView animateWithDuration:animateTime animations:^{ _alertBackgroundView.alpha = 1; }]; /** * 操作区背景 */ _operateView = [[UIView alloc] init]; _operateView.center = CGPointMake(ScreenWidth/2., ScreenHeight/2.); _operateView.bounds = CGRectMake(0, 0, ScreenWidth - 32, 208); _operateView.backgroundColor = [UIColor whiteColor]; _operateView.layer.cornerRadius = 6; _operateView.clipsToBounds = YES; [_alertBackgroundView addSubview:_operateView]; [self shakeToShow:_operateView]; /** * 按钮 */ UIButton * cancelBtn = [self createButtonWithFrame:CGRectMake(0, CGRectGetHeight(_operateView.frame) - 48, _operateView.frame.size.width/2., 48) title:@"取消" andAction:@selector(removeAlertView)]; [cancelBtn setBackgroundImage:[self imageWithColor:UIColorFromHEX(0xffffff, 1) andSize:cancelBtn.bounds.size] forState:UIControlStateNormal]; self.confirmBlock = confirmBlock; UIButton * confirmBtn = [self createButtonWithFrame:CGRectMake(_operateView.frame.size.width/2., CGRectGetHeight(_operateView.frame) - 48, _operateView.frame.size.width/2., 48) title:@"确认" andAction:@selector(clickAction:)]; [confirmBtn setTitleColor:[UIColor colorWithRed:60/255.0 green:114/255.0 blue:255/255.0 alpha:1] forState:UIControlStateNormal]; [confirmBtn setBackgroundImage:[self imageWithColor:UIColorFromHEX(0xffffff, 1) andSize:cancelBtn.bounds.size] forState:UIControlStateNormal]; /** * 分割线 */ UILabel * horLine = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_operateView.frame) - 48 - OnePixel, _operateView.frame.size.width, OnePixel)]; horLine.backgroundColor = UIColorFromHEX(0xcccccc, 1); [_operateView addSubview:horLine]; UILabel * verLine = [[UILabel alloc] initWithFrame:CGRectMake(_operateView.frame.size.width/2. - OnePixel/2., CGRectGetHeight(_operateView.frame) - 48 - OnePixel, OnePixel, 48)]; verLine.backgroundColor = UIColorFromHEX(0xcccccc, 1); [_operateView addSubview:verLine]; /** *标题内容 */ UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_operateView.frame.size.width/2-60, 10,120 , 30)]; titleLabel.text = @"添加联系人"; titleLabel.font = [UIFont systemFontOfSize:18]; titleLabel.textAlignment = NSTextAlignmentCenter; [_operateView addSubview:titleLabel]; /** *姓名 */ UILabel *userLabel= [[UILabel alloc]initWithFrame:CGRectMake(20, _operateView.frame.size.height/2-50, 60, 44)]; userLabel.text =@"姓名:"; userLabel.font = [UIFont systemFontOfSize:18]; [_operateView addSubview:userLabel]; /** *号码 */ UILabel *passLabel= [[UILabel alloc]initWithFrame:CGRectMake(20, _operateView.frame.size.height/2+3, 60, 44)]; passLabel.text =@"号码:"; passLabel.font = [UIFont systemFontOfSize:18]; [_operateView addSubview:passLabel]; /** * 姓名输入框 */ _userTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, _operateView.frame.size.height/2-50, _operateView.frame.size.width-100, 44)]; _userTextField.delegate = self; _userTextField.keyboardType = UIKeyboardTypeDefault; _userTextField.returnKeyType = UIReturnKeyDone; _userTextField.font = [UIFont systemFontOfSize:16]; _userTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入姓名" attributes:@{NSForegroundColorAttributeName:UIColorFromHEX(0xcccccc, 1), NSFontAttributeName:[UIFont systemFontOfSize:16]}]; _userTextField.textColor = UIColorFromHEX(0x333333, 1); _userTextField.clearButtonMode = UITextFieldViewModeWhileEditing; [_operateView addSubview:_userTextField]; UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(80, _operateView.frame.size.height/2-6, _operateView.frame.size.width-100, 0.5)]; line1.backgroundColor = UIColorFromHEX(0xcccccc, 1); [_operateView addSubview:line1]; /** * 密码输入框 */ _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(80, _operateView.frame.size.height/2+3, _operateView.frame.size.width-100, 44)]; _passwordTextField.delegate = self; _passwordTextField.keyboardType = UIKeyboardTypeNumberPad; _passwordTextField.returnKeyType = UIReturnKeyDone; _passwordTextField.font = [UIFont systemFontOfSize:16]; _passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入号码" attributes:@{NSForegroundColorAttributeName:UIColorFromHEX(0xcccccc, 1), NSFontAttributeName:[UIFont systemFontOfSize:16]}]; _passwordTextField.textColor = UIColorFromHEX(0x333333, 1); _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing; [_operateView addSubview:_passwordTextField]; UIView *line2 = [[UIView alloc]initWithFrame:CGRectMake(80, _operateView.frame.size.height/2+47, _operateView.frame.size.width-100, 0.5)]; line2.backgroundColor = UIColorFromHEX(0xcccccc, 1); [_operateView addSubview:line2]; } #pragma mark - 移除视图 - (void)removeAlertView { if ([_userTextField isFirstResponder]) { [_userTextField resignFirstResponder]; } if ([_passwordTextField isFirstResponder]) { [_passwordTextField resignFirstResponder]; } //退出 [UIView animateWithDuration:animateTime animations:^{ _alertBackgroundView.alpha = 0; } completion:^(BOOL finished) { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; if (_notifiKeyboardHide) { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } [self.alertBackgroundView removeFromSuperview]; self.alertBackgroundView = nil; self.operateView = nil; }]; } #pragma mark - 创建按钮 - (UIButton *)createButtonWithFrame:(CGRect)frame title:(NSString *)title andAction:(SEL)action { UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = frame; [btn setTitleColor:UIColorFromHEX(0x333333, 1) forState:UIControlStateNormal]; [btn setTitle:title forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont boldSystemFontOfSize:18]; [btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside]; [_operateView addSubview:btn]; return btn; } - (void)clickAction:(UIButton *)sender { if (self.confirmBlock) { if (_userTextField.text.length ==0||_passwordTextField.text.length ==0) { [self removeAlertView]; UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController; UIViewController *topVC = appRootVC; if (topVC.presentedViewController) { topVC = topVC.presentedViewController; } MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; hud.mode = MBProgressHUDModeText; hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor; hud.bezelView.color = [UIColor grayColor]; hud.label.text = @"姓名或手机号不能为空"; hud.label.textColor = [UIColor whiteColor]; hud.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变 [hud hideAnimated:YES afterDelay:1]; return; }else if(_passwordTextField.text.length !=11){ [self removeAlertView]; UIViewController *appRootVC = [UIApplication sharedApplication].keyWindow.rootViewController; UIViewController *topVC = appRootVC; if (topVC.presentedViewController) { topVC = topVC.presentedViewController; } MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:topVC.view animated:YES]; hud.mode = MBProgressHUDModeText; hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor; hud.bezelView.color = [UIColor grayColor]; hud.label.text = @"请输入正确的手机号"; hud.label.textColor = [UIColor whiteColor]; hud.animationType = MBProgressHUDAnimationFade; //默认类型的,渐变 [hud hideAnimated:YES afterDelay:1]; }else{ NSArray *titlearray = @[_userTextField.text,_passwordTextField.text]; self.confirmBlock(titlearray); [self removeAlertView]; } } } - (void)reloadImageAction:(UIButton *)sender { [self removeAlertView]; } #pragma mark - 监听键盘弹起,操作框动画 ///键盘弹起,页面动画,监听 - (void)keyboardWillShow:(NSNotification *)notification { // 键盘的frame CGRect keyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat keyboardHeight = keyboardRect.size.height; CGFloat keyboardOriginY = ScreenHeight - keyboardHeight; CGFloat operateMaxY = ScreenHeight/2. + _operateView.bounds.size.height/2. + 16; // DLog(@"--- %lf %lf", keyboardOriginY, operateMaxY); if (operateMaxY >= keyboardOriginY) { [UIView animateWithDuration:0.25 animations:^{ CGRect rect = _operateView.frame; rect.origin.y = keyboardOriginY - rect.size.height - 16; _operateView.frame = rect; } completion:^(BOOL finished) { }]; _notifiKeyboardHide = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } else { _notifiKeyboardHide = NO; } } ///键盘收起,页面动画,监听 - (void)keyboardWillHide:(NSNotification *)notification { [UIView animateWithDuration:0.25 animations:^{ CGRect rect = _operateView.frame; rect.origin.y = (ScreenHeight - rect.size.height)/2.; _operateView.frame = rect; } completion:^(BOOL finished) { }]; } #pragma mark - 输入框代理 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; } #pragma mark - 颜色转换为图片 - (UIImage *)imageWithColor:(UIColor *)color andSize:(CGSize)aSize { CGRect rect = CGRectMake(0.0f, 0.0f, aSize.width, aSize.height); UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [color CGColor]); CGContextFillRect(context, rect); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } #pragma mark - 弹性震颤动画 - (void)shakeToShow:(UIView *)aView { CAKeyframeAnimation * popAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform"]; popAnimation.duration = 0.35; popAnimation.values = @[[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.01f, 0.01f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DMakeScale(1.05f, 1.05f, 1.0f)], // [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.9f, 0.9f, 1.0f)], [NSValue valueWithCATransform3D:CATransform3DIdentity]]; popAnimation.keyTimes = @[@0.0f, @0.5f, /*@0.75f,*/ @0.8f]; popAnimation.timingFunctions = @[[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], // [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut], [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; [aView.layer addAnimation:popAnimation forKey:nil]; } @end