// // HDLEZAlertInputView.m // EZSDK // // Created by 陈启扬 on 2023/3/16. // #import "HDLEZAlertInputView.h" //颜色 #define HDLEZAlertHEXCOLOR(rgbValue, a) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16)) / 255.0 green:((float)((rgbValue & 0xFF00) >> 8)) / 255.0 blue:((float)(rgbValue & 0xFF)) / 255.0 alpha:a] //字体 #define HDLEZ_Alert_Get_FontRegularWithSize(s) [UIFont fontWithName:@"PingFangSC-Regular" size:s] @interface HDLEZAlertInputView () @property (atomic, strong) UIView *inputV;//输入view @property (atomic, strong) UILabel *titleL;//标题lable @property (atomic, strong) UITextField *inputTextF;//输入textField @property (atomic, strong) UIButton *cancleBtn;//取消button @property (atomic, strong) UIButton *sureBtn;//确认button @property(nonatomic,strong)HDLEZAlertInputViewCancleBlock cancleBlock; //取消回调 @property(nonatomic,strong)HDLEZAlertInputViewSureBlock sureBlock; //确认回调 @end @implementation HDLEZAlertInputView /*展示输入弹窗 @param title 标题 @param cancleTitle 删除按钮标题 @param sureTitle 确认按钮标题 */ +(void)showInputAlertWithtitle:(NSString*)title cancleTitle:(NSString*)cancleTitle sureTitle:(NSString*)sureTitle keyboardType:(UIKeyboardType)keyboardType cancleBlock:(HDLEZAlertInputViewCancleBlock)cancle sureBlock:(HDLEZAlertInputViewSureBlock)sure{ [[[[UIApplication sharedApplication] windows] lastObject] makeKeyWindow]; [self showInputAlertToView:[[[UIApplication sharedApplication] windows] lastObject] title:title cancleTitle:cancleTitle sureTitle:sureTitle keyboardType:keyboardType cancleBlock:cancle sureBlock:sure]; } /*展示输入弹窗 */ +(void)showInputAlertToView:(UIView*)view title:(NSString*)title cancleTitle:(NSString*)cancleTitle sureTitle:(NSString*)sureTitle keyboardType:(UIKeyboardType)keyboardType cancleBlock:(HDLEZAlertInputViewCancleBlock)cancle sureBlock:(HDLEZAlertInputViewSureBlock)sure{ HDLEZAlertInputView *alert = [[self alloc] initWithView:view]; alert.titleL.text=title; [alert.cancleBtn setTitle:cancleTitle forState:UIControlStateNormal]; [alert.sureBtn setTitle:sureTitle forState:UIControlStateNormal]; alert.inputTextF.keyboardType=keyboardType; alert.cancleBlock=cancle; alert.sureBlock=sure; [view addSubview:alert]; [alert.inputTextF becomeFirstResponder];//进入编辑状态 } /*初始化view */ - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { //背景 UIView *backV=[[UIView alloc] initWithFrame:frame]; backV.backgroundColor=[UIColor blackColor]; backV.alpha=0.6; [self addSubview:backV]; //输入view CGFloat inputV_Width=frame.size.width-104; CGFloat inputV_Height=inputV_Width*0.64; _inputV=[[UIView alloc] initWithFrame:CGRectMake(52,(frame.size.height-inputV_Height)/2, inputV_Width, inputV_Height)]; _inputV.backgroundColor=HDLEZAlertHEXCOLOR(0xFFFFFF,1.0); _inputV.layer.cornerRadius=10; _inputV.layer.masksToBounds=YES; [self addSubview:_inputV]; //标题 _titleL=[[UILabel alloc] initWithFrame:CGRectMake(0, 20, inputV_Width, 24)]; _titleL.textAlignment=NSTextAlignmentCenter; _titleL.textColor=HDLEZAlertHEXCOLOR(0x4484F4,1.0); _titleL.font=HDLEZ_Alert_Get_FontRegularWithSize(16); [_inputV addSubview:_titleL]; //输入框 _inputTextF=[[UITextField alloc] initWithFrame:CGRectMake(24, (inputV_Height-40)/2, inputV_Width-48, 40)]; _inputTextF.textColor=HDLEZAlertHEXCOLOR(0x1B2D4D,1.0); _inputTextF.font=HDLEZ_Alert_Get_FontRegularWithSize(14); _inputTextF.backgroundColor=HDLEZAlertHEXCOLOR(0xF2F3F7,1.0); _inputTextF.layer.cornerRadius=4; _inputTextF.clearButtonMode=UITextFieldViewModeWhileEditing; [_inputV addSubview:_inputTextF]; //取消按钮 _cancleBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, inputV_Height-((inputV_Width/2)*0.33), inputV_Width/2, (inputV_Width/2)*0.33)]; [_cancleBtn setTitleColor:HDLEZAlertHEXCOLOR(0x798394,1.0) forState:UIControlStateNormal]; _cancleBtn.titleLabel.font=HDLEZ_Alert_Get_FontRegularWithSize(16); [_cancleBtn addTarget:self action:@selector(clickCancle) forControlEvents:UIControlEventTouchUpInside]; [_inputV addSubview:_cancleBtn]; //确定按钮 _sureBtn=[[UIButton alloc] initWithFrame:CGRectMake(inputV_Width/2, inputV_Height-((inputV_Width/2)*0.33), inputV_Width/2, (inputV_Width/2)*0.33)]; [_sureBtn setTitleColor:HDLEZAlertHEXCOLOR(0xFFFFFF,1.0) forState:UIControlStateNormal]; [_sureBtn setBackgroundColor:HDLEZAlertHEXCOLOR(0x4484F4,1.0)]; _sureBtn.titleLabel.font=HDLEZ_Alert_Get_FontRegularWithSize(16); [_sureBtn addTarget:self action:@selector(clickSure) forControlEvents:UIControlEventTouchUpInside]; [_inputV addSubview:_sureBtn]; //横线 UIView *lineV=[[UIView alloc] initWithFrame:CGRectMake(0, _sureBtn.frame.origin.y-0.3,inputV_Width/2 , 0.6)]; lineV.backgroundColor=HDLEZAlertHEXCOLOR(0x08141F,1.0); //添加点击屏幕手势 UITapGestureRecognizer *tapGr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector (viewTapped)]; tapGr.cancelsTouchesInView = NO ; [backV addGestureRecognizer:tapGr]; //注册键盘通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillhide:) name:UIKeyboardWillHideNotification object:nil]; } return self; } -(void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } //初始化alertView - (id)initWithView:(UIView *)view { return [self initWithFrame:view.bounds]; } /*点击取消 */ -(void)clickCancle{ if (self.cancleBlock) { self.cancleBlock(); } [self removeFromSuperview]; } /*点击确认 */ -(void)clickSure{ if (self.sureBlock) { self.sureBlock(self.inputTextF.text); } [self removeFromSuperview]; } /*点击背景view */ -(void)viewTapped{ // //键盘退出 [self endEditing:YES]; HDLEZLog(@"点击了背景"); // [_inputTextF becomeFirstResponder];//进入编辑状态 } /*将展示键盘 */ -(void)keyBoardWillShow:(NSNotification*)notification{ //获取键盘弹出的时间 double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //获取键盘上端Y坐标 CGFloat keyboardY = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y; //获取输入框下端相对于window的Y坐标 CGRect rect = [_inputV convertRect:_inputV.bounds toView:[self appWindow]]; CGPoint tmp = rect.origin; CGFloat inputBoxY = tmp.y + _inputV.frame.size.height; //计算二者差值 CGFloat ty = keyboardY - inputBoxY-20; // NSLog(@"position keyboard: %f, inputbox: %f, ty: %f", keyboardY, inputBoxY, ty); //差值小于0,做平移变换 __weak __typeof(self)weakSelf = self; [UIView animateWithDuration:duration animations:^{ if (ty < 0) { weakSelf.inputV.transform = CGAffineTransformMakeTranslation(0, ty); } }]; } /*将关闭键盘 */ -(void)keyBoardWillhide:(NSNotification*)notification{ //获取键盘弹出的时间 double duration = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; //还原 __weak __typeof(self)weakSelf = self; [UIView animateWithDuration:duration animations:^{ weakSelf.inputV.transform = CGAffineTransformMakeTranslation(0, 0); }]; } /*获取单前window */ -(UIWindow*)appWindow{ UIWindow *window; if (@available(iOS 13.0, *)) { if ([UIApplication sharedApplication].delegate.window) { window=[UIApplication sharedApplication].delegate.window; }else{ window = [UIApplication sharedApplication].windows[0]; } } else { window = [UIApplication sharedApplication].delegate.window; } return window; } @end