自定义带输入框类型AlertView

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

代码直接拷贝可用:

#import <UIKit/UIKit.h>

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


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 157,298评论 4 360
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 66,701评论 1 290
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 107,078评论 0 237
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,687评论 0 202
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,018评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,410评论 1 211
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,729评论 2 310
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,412评论 0 194
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,124评论 1 239
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,379评论 2 242
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,903评论 1 257
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,268评论 2 251
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,894评论 3 233
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,014评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,770评论 0 192
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,435评论 2 269
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,312评论 2 260

推荐阅读更多精彩内容