自定义带输入框类型AlertView

由于系统自带AlertView带输入框样式不太美观,决定自定义类似系统带有输入框样式AlertView,可根据需求自行修改样式,使用极其方便,同时已经做好了键盘事件处理。

代码直接拷贝可用:

#import

typedefvoid(^ClickBlock)(NSString* inputText);

typedefvoid(^ReloadBlock)();

@interfaceInputAlertView :UIView

+ (InputAlertView*)sharedAlertView;

- (void)showAlertViewTitle:(NSString*)title withConfirmAction:(ClickBlock)confirmBlock;

@property(nonatomic,strong)NSString*placeholder;

@end

#import "InputAlertView.h"

#define ScreenWidth[[UIScreen mainScreen] bounds].size.width

#define ScreenHeight [[UIScreen mainScreen] bounds].size.height

#define OnePixel(1./[UIScreen mainScreen].scale)

#define animateTime0.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]

@interfaceInputAlertView()

@property(nonatomic,assign)BOOLnotifiKeyboardHide;

@property(nonatomic,strong)UITextField* inputTextField;//输入框

@property(nonatomic,strong)UIView* alertBackgroundView;

@property(nonatomic,strong)UIView* operateView;//操作视图

@property(nonatomic,strong)UILabel* titleLabel;//标题

@property(nonatomic,copy)ClickBlockconfirmBlock;

@property(nonatomic,copy)ReloadBlockreloadBlock;

@end

@implementationInputAlertView

+ (InputAlertView*)sharedAlertView

{

staticdispatch_once_tonce;

staticInputAlertView* _alertView =nil;

dispatch_once(&once, ^{

if(_alertView ==nil) {

_alertView = [[selfalloc]init];

}

});

return_alertView;

}

#pragma mark - 创建UI

- (void)showAlertViewTitle:(NSString*)title withConfirmAction:(ClickBlock)confirmBlock

{

_notifiKeyboardHide=NO;

[[NSNotificationCenterdefaultCenter]addObserver:self

selector:@selector(keyboardWillShow:)

name:UIKeyboardWillShowNotification

object:nil];

/**

*背景视图

*/

_alertBackgroundView= [[UIViewalloc]initWithFrame:CGRectMake(0,0,ScreenWidth,ScreenHeight)];

_alertBackgroundView.backgroundColor=UIColorFromHEX(0x000000,0.7);

[[UIApplicationsharedApplication].keyWindowaddSubview:_alertBackgroundView];

_alertBackgroundView.alpha=0;

[UIViewanimateWithDuration:animateTimeanimations:^{

_alertBackgroundView.alpha=1;

}];

/**

*操作区背景

*/

_operateView= [[UIViewalloc]init];

_operateView.center=CGPointMake(ScreenWidth/2.,ScreenHeight/2.);

_operateView.bounds=CGRectMake(0,0,ScreenWidth-32,170);

_operateView.backgroundColor= [UIColorwhiteColor];

_operateView.layer.cornerRadius=6;

_operateView.clipsToBounds=YES;

[_alertBackgroundViewaddSubview:_operateView];

[selfshakeToShow:_operateView];

/**

*按钮

*/

UIButton* cancelBtn = [selfcreateButtonWithFrame:CGRectMake(0,CGRectGetHeight(_operateView.frame) -48,_operateView.frame.size.width/2.,48)title:@"取消"andAction:@selector(removeAlertView)];

[cancelBtnsetTitleColor:[UIColorlightGrayColor]forState:UIControlStateNormal];

//[cancelBtn setBackgroundImage:[self imageWithColor:UIColorFromHEX(0xffffff, 1) andSize:cancelBtn.bounds.size] forState:UIControlStateNormal];

self.confirmBlock= confirmBlock;

UIButton* confirmBtn = [selfcreateButtonWithFrame:CGRectMake(_operateView.frame.size.width/2.,CGRectGetHeight(_operateView.frame) -48,_operateView.frame.size.width/2.,48)title:@"确定"andAction:@selector(clickAction:)];

[confirmBtnsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

//[confirmBtn setBackgroundImage:[self imageWithColor:UIColorFromHEX(0xffffff, 1) andSize:cancelBtn.bounds.size] forState:UIControlStateNormal];

/**

*分割线

*/

UILabel* horLine = [[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetHeight(_operateView.frame) -48-OnePixel,_operateView.frame.size.width,OnePixel)];

horLine.backgroundColor=UIColorFromHEX(0xcccccc,1);

[_operateViewaddSubview:horLine];

UILabel* verLine = [[UILabelalloc]initWithFrame:CGRectMake(_operateView.frame.size.width/2.-OnePixel/2.,CGRectGetHeight(_operateView.frame) -48-OnePixel,OnePixel,48)];

verLine.backgroundColor=UIColorFromHEX(0xcccccc,1);

[_operateViewaddSubview:verLine];

/**

*标题

*/

_titleLabel= [[UILabelalloc]initWithFrame:CGRectMake(0,0,_operateView.frame.size.width,48)];

_titleLabel.font= [UIFontsystemFontOfSize:18];

_titleLabel.textColor= [UIColorredColor];

_titleLabel.text= title;

_titleLabel.textAlignment=NSTextAlignmentCenter;

[_operateViewaddSubview:_titleLabel];

/**

*分割线1

*/

UILabel* horLine1 = [[UILabelalloc]initWithFrame:CGRectMake(0,CGRectGetHeight(_titleLabel.frame) -OnePixel,_operateView.frame.size.width,OnePixel)];

horLine1.backgroundColor=UIColorFromHEX(0xcccccc,1);

[_operateViewaddSubview:horLine1];

/**

*输入框背景

*/

UIView* inputBkView = [[UIViewalloc]init];

//inputBkView.layer.borderColor = UIColorFromHEX(0xcccccc, 1).CGColor;

//inputBkView.layer.borderWidth = 1;

inputBkView.bounds=CGRectMake(0,0,CGRectGetWidth(_operateView.frame) -20,48);

inputBkView.center=CGPointMake(CGRectGetMidX(_operateView.bounds),CGRectGetMidY(_operateView.bounds));

[_operateViewaddSubview:inputBkView];

/**

*输入框

*/

_inputTextField= [[UITextFieldalloc]initWithFrame:CGRectMake(16,0,CGRectGetWidth(inputBkView.bounds) -10,CGRectGetHeight(inputBkView.bounds))];

_inputTextField.delegate=self;

_inputTextField.keyboardType=UIKeyboardTypeDefault;

_inputTextField.returnKeyType=UIReturnKeyDone;

_inputTextField.font= [UIFontsystemFontOfSize:16];

_inputTextField.attributedPlaceholder= [[NSAttributedStringalloc]initWithString:@"在此输入内容"attributes:@{NSForegroundColorAttributeName:UIColorFromHEX(0xcccccc,1),NSFontAttributeName:[UIFontsystemFontOfSize:16]}];

_inputTextField.textColor=UIColorFromHEX(0x333333,1);

_inputTextField.clearButtonMode=UITextFieldViewModeWhileEditing;

[inputBkViewaddSubview:_inputTextField];

[_inputTextFieldbecomeFirstResponder];

}

- (void)setPlaceholder:(NSString*)placeholder {

_placeholder= placeholder;

_inputTextField.attributedPlaceholder= [[NSAttributedStringalloc]initWithString:placeholderattributes:@{NSForegroundColorAttributeName:UIColorFromHEX(0xcccccc,1),NSFontAttributeName:[UIFontsystemFontOfSize:16]}];

}

#pragma mark - 移除视图

- (void)removeAlertView

{

if([_inputTextFieldisFirstResponder]) {

[_inputTextFieldresignFirstResponder];

}

//退出

[UIViewanimateWithDuration:animateTimeanimations:^{

_alertBackgroundView.alpha=0;

}completion:^(BOOLfinished) {

[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];

if(_notifiKeyboardHide) {

[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];

}

[self.alertBackgroundViewremoveFromSuperview];

self.alertBackgroundView=nil;

self.operateView=nil;

}];

}

#pragma mark - 创建按钮

- (UIButton*)createButtonWithFrame:(CGRect)frame title:(NSString*)title andAction:(SEL)action

{

UIButton* btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

btn.frame= frame;

[btnsetTitleColor:UIColorFromHEX(0x333333,1)forState:UIControlStateNormal];

[btnsetTitle:titleforState:UIControlStateNormal];

btn.titleLabel.font= [UIFontboldSystemFontOfSize:16];

[btnaddTarget:selfaction:actionforControlEvents:UIControlEventTouchUpInside];

[_operateViewaddSubview:btn];

returnbtn;

}

- (void)clickAction:(UIButton*)sender

{

if(self.confirmBlock) {

self.confirmBlock(_inputTextField.text);

}

[selfremoveAlertView];

}

- (void)reloadImageAction:(UIButton*)sender

{

if(self.reloadBlock) {

self.reloadBlock();

}

}

#pragma mark - 监听键盘弹起,操作框动画

///键盘弹起,页面动画,监听

- (void)keyboardWillShow:(NSNotification*)notification

{

// 键盘的frame

CGRectkeyboardRect = [notification.userInfo[UIKeyboardFrameEndUserInfoKey]CGRectValue];

CGFloatkeyboardHeight = keyboardRect.size.height;

CGFloatkeyboardOriginY =ScreenHeight- keyboardHeight;

CGFloatoperateMaxY =ScreenHeight/2.+_operateView.bounds.size.height/2.+16;

//DLog(@"--- %lf %lf", keyboardOriginY, operateMaxY);

if(operateMaxY >= keyboardOriginY) {

[UIViewanimateWithDuration:0.25animations:^{

CGRectrect =_operateView.frame;

rect.origin.y= keyboardOriginY - rect.size.height-16;

_operateView.frame= rect;

}completion:^(BOOLfinished) {

}];

_notifiKeyboardHide=YES;

[[NSNotificationCenterdefaultCenter]addObserver:self

selector:@selector(keyboardWillHide:)

name:UIKeyboardWillHideNotification

object:nil];

}

else{

_notifiKeyboardHide=NO;

}

}

///键盘收起,页面动画,监听

- (void)keyboardWillHide:(NSNotification*)notification

{

[UIViewanimateWithDuration:0.25animations:^{

CGRectrect =_operateView.frame;

rect.origin.y= (ScreenHeight- rect.size.height)/2.;

_operateView.frame= rect;

}completion:^(BOOLfinished) {

}];

}

#pragma mark - 输入框代理

- (BOOL)textFieldShouldReturn:(UITextField*)textField

{

[textFieldresignFirstResponder];

returnYES;

}

#pragma mark - 颜色转换为图片

- (UIImage*)imageWithColor:(UIColor*)color andSize:(CGSize)aSize

{

CGRectrect =CGRectMake(0.0f,0.0f, aSize.width, aSize.height);

UIGraphicsBeginImageContext(rect.size);

CGContextRefcontext =UIGraphicsGetCurrentContext();

CGContextSetFillColorWithColor(context, [colorCGColor]);

CGContextFillRect(context, rect);

UIImage*image =UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

returnimage;

}

#pragma mark - 弹性震颤动画

- (void)shakeToShow:(UIView*)aView

{

CAKeyframeAnimation* popAnimation = [CAKeyframeAnimationanimationWithKeyPath:@"transform"];

popAnimation.duration=0.35;

popAnimation.values=@[[NSValuevalueWithCATransform3D:CATransform3DMakeScale(0.01f,0.01f,1.0f)],

[NSValuevalueWithCATransform3D:CATransform3DMakeScale(1.05f,1.05f,1.0f)],

[NSValuevalueWithCATransform3D:CATransform3DIdentity]];

popAnimation.keyTimes=@[@0.0f,@0.5f,/*@0.75f,*/@0.8f];

popAnimation.timingFunctions=@[[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut],

[CAMediaTimingFunctionfunctionWithName:kCAMediaTimingFunctionEaseInEaseOut]];

[aView.layeraddAnimation:popAnimationforKey:nil];

}

@end


自定义带输入框类型AlertView_第1张图片

你可能感兴趣的:(自定义带输入框类型AlertView)