本文转载至 http://www.itnose.net/detail/6145865.html

最近要在系统弹出的数字键盘上的左下角额外添加一个自定制的完成按钮,于是研究了一下系统自带键盘添加自定制按钮的实现方式。总结了一下大体上的通用做法,原理大概是这样:当页面上的文本框或其他输入源因为用户的点击而变成第一响应者的时候(becomeFirstResponder),系统键盘就会弹出。而每次键盘弹出或收起时,都会向系统发送相关的键盘事件即通知消息(notification)。所以,我们只要在键盘弹出或收起时捕获相关的键盘事件,并且在键盘对应的window上的相应位置添加或移除我们自定制的按钮即可。

按照这种思路,在键盘弹出时添加上我们自定制的按钮是完全可行的。但是,如果只是这样简单的处理,造成的效果是:键盘弹出和自定制按钮的添加是不同步的。有时候键盘还没有弹出自定制的按钮就已经加在试图上了,有时候键盘弹出后自定制的按钮才添加上去。那么我们如何使得键盘的弹出和自定制按钮的添加能同步进行呢,要完成这种平滑的过度弹出效果,还需要获得键盘弹出的动画时间和动画类型,然后让自定制按钮的添加动画和键盘的弹出、收起动作同步。

1、首先在 viewWillAppear 方法中注册监听相应的键盘通知,并且要在 viewWillDisappear 方法中注销通知

- (void)viewWillAppear:(BOOL)animated {

    //注册键盘显示通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardDidShow:) name:UIKeyboardWillShowNotification object:nil];
//注册键盘隐藏通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleKeyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [super viewWillAppear:animated];
}
-(void)viewWillDisappear:(BOOL)animated{

    //注销键盘显示通知
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; [super viewWillDisappear:animated];
}

2、处理键盘弹出和收起事件

// 键盘出现处理事件
- (void)handleKeyboardDidShow:(NSNotification *)notification
{
// NSNotification中的 userInfo字典中包含键盘的位置和大小等信息
NSDictionary *userInfo = [notification userInfo];
// UIKeyboardAnimationDurationUserInfoKey 对应键盘弹出的动画时间
CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
// UIKeyboardAnimationCurveUserInfoKey 对应键盘弹出的动画类型
NSInteger animationCurve = [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] integerValue];
//数字彩,数字键盘添加“完成”按钮
if (doneInKeyboardButton){ [UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:animationDuration];//设置添加按钮的动画时间
[UIView setAnimationCurve:(UIViewAnimationCurve)animationCurve];//设置添加按钮的动画类型 //设置自定制按钮的添加位置(这里为数字键盘添加“完成”按钮)
doneInKeyboardButton.transform=CGAffineTransformTranslate(doneInKeyboardButton.transform, 0, -53); [UIView commitAnimations];
} }
// 键盘消失处理事件
- (void)handleKeyboardWillHide:(NSNotification *)notification
{
// NSNotification中的 userInfo字典中包含键盘的位置和大小等信息
NSDictionary *userInfo = [notification userInfo];
// UIKeyboardAnimationDurationUserInfoKey 对应键盘收起的动画时间
CGFloat animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; if (doneInKeyboardButton.superview)
{
[UIView animateWithDuration:animationDuration animations:^{
//动画内容,将自定制按钮移回初始位置
doneInKeyboardButton.transform=CGAffineTransformIdentity;
} completion:^(BOOL finished) {
//动画结束后移除自定制的按钮
[doneInKeyboardButton removeFromSuperview];
}]; }
}

3、点击输入框,初始化自定制按钮并弹出键盘

//点击输入框
- (IBAction)editingDidBegin:(id)sender{ //初始化数字键盘的“完成”按钮
[self configDoneInKeyBoardButton];
}
//初始化,数字键盘“完成”按钮
- (void)configDoneInKeyBoardButton{
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
//初始化
if (doneInKeyboardButton == nil)
{
doneInKeyboardButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
[doneInKeyboardButton setTitle:@"完成" forState:UIControlStateNormal];
[doneInKeyboardButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; doneInKeyboardButton.frame = CGRectMake(0, screenHeight, 106, 53); doneInKeyboardButton.adjustsImageWhenHighlighted = NO;
[doneInKeyboardButton addTarget:self action:@selector(finishAction) forControlEvents:UIControlEventTouchUpInside];
}
//每次必须从新设定“完成”按钮的初始化坐标位置
doneInKeyboardButton.frame = CGRectMake(0, screenHeight, 106, 53); //由于ios8下,键盘所在的window视图还没有初始化完成,调用在下一次 runloop 下获得键盘所在的window视图
[self performSelector:@selector(addDoneButton) withObject:nil afterDelay:0.0f]; } - (void) addDoneButton{
//获得键盘所在的window视图
UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
[tempWindow addSubview:doneInKeyboardButton]; // 注意这里直接加到window上 }
//点击“完成”按钮事件,收起键盘
-(void)finishAction{
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];//关闭键盘
}

按照这种思路和实现原理,可以在键盘上方等地方添加输入框等其他自定制控件并且可以跟随键盘的弹出和收起平滑过度出现。

ios在数字键盘左下角添加“完成”按钮的实现原理的更多相关文章

  1. iOS 系统数字键盘左下角加确定按钮

    首先在 viewWillAppear 方法中注册监听相应的键盘通知,并且要在 viewWillDisappear 方法中注销通知- (void)viewWillAppear:(BOOL)animate ...

  2. ios自定义数字键盘

    因为项目又一个提现的功能,textfiled文本框输入需要弹出数字键盘,首先想到的就是设置textfiled的keyboardType为numberPad,此时你会看到如下的效果:   但是很遗憾这样 ...

  3. iOS为数字键盘增加完成按钮

    在输入价格的时候,要求弹出的键盘只能有数字和小数点.弹出的键盘没有完成键,想要退出键盘可以点击退出,但是为了更好的用户体验,在键盘上增加UIToolbar. 设置ToolBar: - (UIToolb ...

  4. textview 弹出键盘上面添加完成按钮,并设置输入内容的格式。

    - (void)setContentView{ self.contentTextView = [[UITextView alloc]initWithFrame:CGRectMake(11, 70, S ...

  5. UIKeyboardTypeNumberPad 数字键盘添加完成按钮

    一:添加通知 //数字键盘添加完成 [[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(keyboardWi ...

  6. iOS数字键盘自定义按键

    UIKeyboardTypeNumberPad 数字键盘自定义按键 最近做一个搜索用户的功能,这里使用了UISearchBar.由于搜索的方式只有手机号码,所以这里的键盘要限制为数字输入,可以这么做: ...

  7. 【笔记】移动端H5数字键盘input type=number的处理(IOS和Android)

    在Vue中的项目,基于VUX-UI开发,一个常见的需求: 1.金额输入框 2.弹出数字键盘 3.仅支持输入两位小数,限制最大11位数,不允许0开头 后续:与UI沟通后, 思路调整为限制输入,并减少正则 ...

  8. iOS 键盘添加完成按钮,delegate和block回调

    这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...

  9. iOS 为键盘添加隐藏按钮

    // 为键盘添加隐藏按钮 UIToolbar * backView = [[UIToolbar alloc]initWithFrame:CGRectMake(, , , )]; [backView s ...

随机推荐

  1. 【DB2】NVL2函数

    语法: NVL2(表达式1,表达式2,表达式3) 如果表达式1为空,返回值为表达式3的值.如果表达式1不为空,返回值为表达式2的值. 例子: SELECT ID, NVL2(SEX,'非空','空值' ...

  2. LaTeX 在编译时出现 File ended while scanning use of \@writefile错误

    LaTeX -在修改论文过程中,重新编译时.出现了File ended while scanning use of \@writefile错误,如以下所示: 问题出现的原因: 因为aux文件没有完整输 ...

  3. windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决

    windows中修改catalina.sh上传到linux执行报错This file is needed to run this program解决 一.发现问题 由于tomcat内存溢出,在wind ...

  4. bootstrap 学习笔记 轮播(Carousel)插件

    Bootstrap轮播(carousel)插件是一种灵活的响应式的向站点添加滑块的方式.除些之外,内容也是足够灵活的,可以是图像,内嵌框架,视频或者其他您想要旋转的任何类型的内容. 示例: 下面是不念 ...

  5. JS关于scrollTop和可视区域clientWidth

    一.scrollTop,scrollLeft 要获得页面的scrollTop及scrollLeft,在不同的浏览器中是不一样的: 谷歌浏览器和没声明DTD的文档,通过document.body.scr ...

  6. Paper Reading 1 - Playing Atari with Deep Reinforcement Learning

    来源:NIPS 2013 作者:DeepMind 理解基础: 增强学习基本知识 深度学习 特别是卷积神经网络的基本知识 创新点:第一个将深度学习模型与增强学习结合在一起从而成功地直接从高维的输入学习控 ...

  7. c#.net调用pdf2swf.exe将pdf文件转换为swf,vs中运行正常,布署IIS服务器部署转换后文字部分为空白

    这个是权限问题, 需要在应用程序池中高级设置,将标识改为LocalSystem

  8. 静态资源打包:一个javescript 的src引用多个文件,一个link引用多个CSS文件

    疑惑描述: 查看了淘宝网的首页源文件,看到这样的一个特殊的 <script src="http://a.tbcdn.cn/??s/kissy/1.1.6/kissy-min.js,p/ ...

  9. Verilog语言:还真的是人格分裂的语言

    人气腹语术师天愿在现场披露了被人偶搭档夺取灵魂的腹语术师将妻子杀害的表演节目.天愿真的陷入了多重人格,命令自己杀害妻子和子的人偶的人格出现了.为了不(让自己)杀害和弟子登川有外遇的妻子,天愿提出委托想 ...

  10. Entity Framework "There is already an open DataReader associated with this 的解决办法

    解决办法: 1,修改连接串,加上MultipleActiveResultSets=true 2, 一次性先把数据读出来 var contacts = from c in db.Contact sele ...