AJ分享,必须精品

先看效果

制作思路

在做这个的时候,首先用stroyboard画出来界面UI,这个很简单,不多说了,然后下一步就是自定义xib做键盘上面的那一栏了,需要自己做xib还有view,详细代码下面会有,当我们点击的时候,我们的键盘的frame变化形成弹出的效果,这个在前面QQ案例里面有,其实就是通知的用法,其中我们自定义工具条的“上一个”“下一个”“完成”这些按钮,我们用了代理的方式来完成的。

代码,注意点

代理

首先我们要自己写自己的代理:

@protocol NYKeyboardToolDelegate <NSObject> 

-(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;

@end

然后要在我们设置代理的类那让他实现我们的协议

@interface NYViewController () <NYKeyboardToolDelegate>

创建工具栏 ,设置代理,把所有的UITextField遍历到放到全局变量_fields中来,设置属性

//创建工具栏
NYKeyboardTool *keyboardTool = [NYKeyboardTool keyBoardTool]; //设置代理
keyboardTool.delegate = self; //1,获取输入框容器的所有子控件
NSArray *views = [self.inputContainer subviews];
//创建一个数组储存textField
NSMutableArray *fieldM = [NSMutableArray array];
//2,遍历
for (UIView *childView in views) {
//如果子控件是UITextField的时候,设置inputAccessoryView
if ([childView isKindOfClass:[UITextField class]]) {
UITextField *tf = (UITextField *)childView;
tf.inputAccessoryView = keyboardTool;
//把textField添加到数组中。
[fieldM addObject:tf];
_fields = fieldM;
}
}

记住要实现协议方法否则工具条按钮没用

#pragma mark - NYKeyboardToolDelegate代理 键盘工具条的代理

-(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType
{ //获取当前的响应者的索引
int currentIndex = [self getCurrentResponderIndex];
NSLog(@"当前的响应者 %d",currentIndex); if (itemType == KeyboardItemTypePrevious) {
NSLog(@"上一个");
//让上一个field成功响应者
[self showProviousField:currentIndex];
}else if(itemType == KeyboardItemTypeNext){
NSLog(@"下一个"); //让下一个field成功响应者
[self showNextField:currentIndex];
}else{
NSLog(@"完成");
[self touchesBegan:nil withEvent:nil];
}
} //让上一个field成功响应者
-(void)showProviousField:(int)currentIndex{
int proviousIndex = currentIndex - 1; if (proviousIndex >= 0 ) {
UITextField *proviousTf = [_fields objectAtIndex:proviousIndex];
[proviousTf becomeFirstResponder];
} } //让下一个field成功响应者
-(void)showNextField:(int)currentIndex{
int nextIndex = currentIndex + 1; //下一个索引不能超过_fields数组的个数
if (nextIndex < _fields.count) {
//让当前响应者分发去
UITextField *currentTf = [_fields objectAtIndex:currentIndex];
[currentTf resignFirstResponder]; UITextField *nextTf = [_fields objectAtIndex:nextIndex];
[nextTf becomeFirstResponder];
} }

键盘的frame变化动画,还有通知

初始化自定义键盘

//1:初始化自定义键盘
-(void)setupCustomKeyboard
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init]; //设置地区
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; //设置模式
datePicker.datePickerMode = UIDatePickerModeDate; //把datePicker放到birthday的键盘中
self.birthdayField.inputView = datePicker; }

键盘frame变化除了,开通通知
建立监听(通知)

//建立监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];

键盘frame的变化

//键盘的frame的变换
-(void)kbFrameChange:(NSNotification *)noti
{ //改变window的背景颜色
self.view.window.backgroundColor = self.inputAccessoryView.backgroundColor; // 键盘退出的frame
CGRect frame = [noti.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; //键盘实时y
CGFloat kbEndY = frame.origin.y; //获取当前的响应者
int currentIndex = [self getCurrentResponderIndex];
UITextField *currentTF = _fields[currentIndex]; //应该改变的高度:
CGFloat tfMaxY = CGRectGetMaxY(currentTF.frame) + self.inputContainer.frame.origin.y;
//如果textfield的最大值在于键盘的y坐,才要往上移
if (tfMaxY > kbEndY) {
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, kbEndY - tfMaxY);
}];
}else{
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
}

其他小地方

关闭键盘

//开始触摸的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//关闭键盘
[self.view endEditing:YES];
}

view中又很多UITextField 然后我们想知道哪一个打开了键盘,就是获取哪一个是响应者

//获取当前  textField的响应者在_fields中的索引
//返回-1代表没有找到
-(int)getCurrentResponderIndex
{
//遍历所有的textField获取响应值
for (UITextField *tf in _fields) {
if (tf.isFirstResponder) {
return [_fields indexOfObject:tf];
}
}
return -1;
}

UIDatePicker日期键盘的用法:

    UIDatePicker *datePicker = [[UIDatePicker alloc] init];

    //设置地区
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; //设置模式
datePicker.datePickerMode = UIDatePickerModeDate; //把datePicker放到birthday的键盘中
self.birthdayField.inputView = datePicker;

最新文章

  1. effectiveC++ 内存管理 学习笔记
  2. vaadin_demo
  3. javascript回文和类名的检测方法
  4. javascript实用技巧,js小知识
  5. JS的学习体会与分享
  6. Javascript验证用户输入URL地址是否正确
  7. C#常用简单线程实例
  8. 使用ptrace向已运行进程中注入.so并执行相关函数
  9. javascript为目标标签添加class样式
  10. Varnish CentOS 6.4 x64
  11. 关于js 构造 onclick 方法中传递Guid参数问题
  12. Golang访问Redis初体验
  13. POJ 3085 - Quick Change
  14. Linux-高可用Keepalived概念篇
  15. MVC Views文件夹下js无法访问问题解决方案
  16. Java循环一个对象的所有属性,并通过反射给这些属性赋值/取值
  17. Oracle 学习总结 - 问题诊断
  18. mac os idea的快捷键
  19. 4518: [Sdoi2016]征途
  20. ANDROID教程目录

热门文章

  1. MyBatis框架——延迟加载
  2. 新文预览 | IoU-aware Single-stage Object Detector for Accurate Localization
  3. identityserver4源码解析_3_认证接口
  4. SpringMVC最详细笔记partⅠ
  5. TensorFlow 多元线性回归【波士顿房价】
  6. 图-最短路-dijkstra-0/1BFS-1368. 使网格图至少有一条有效路径的最小代价
  7. 动态规划-Distinct Subsequences
  8. DL 调参经验
  9. latex中文支持ubuntu
  10. TensorFlow 卷积神经网络手写数字识别数据集介绍