iOS 数字键盘完成键
2016-04-25 10:31
429 查看
1、首先在 viewWillAppear 方法中注册监听相应的键盘通知,并且要在 viewWillDisappear 方法中注销通知
2、处理键盘弹出和收起事件
3、点击输入框,初始化自定制按钮并弹出键盘
- (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集合
- iOS 压缩与裁剪图片问题
- ios富文本设置
- iOS中的三种多线程
- 逗视 iOS客户端应用源码
- iOS实用知识点总结
- ios常用框架
- 按钮倒计时
- iOS开发证书问题
- iOS 如何画圆
- iOS:编译错误[__NSDictionaryM ....]: unrecognized selector sent to instance 0xa79e61
- 如何制作BUNDLE
- 【代码笔记】iOS-iCarouselDemo
- iOS开发~CocoaPods使用详细说明
- iOS开发:关于NSUserDefault的用法
- 自学iOS-获取当前时间
- iOS基础-属性与成员变量的区别
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
- iOS开发系列--通知与消息机制
- iOS开发系列--触摸事件、手势识别、摇晃事件、耳机线控