AJ学IOS(24)UI之注册案例
2015-11-29 00:41
441 查看
AJ分享,必须精品
然后要在我们设置代理的类那让他实现我们的协议
创建工具栏 ,设置代理,把所有的UITextField遍历到放到全局变量_fields中来,设置属性
记住要实现协议方法否则工具条按钮没用
键盘frame变化除了,开通通知
建立监听(通知)
键盘frame的变化
view中又很多UITextField 然后我们想知道哪一个打开了键盘,就是获取哪一个是响应者
UIDatePicker日期键盘的用法:
先看效果
***思路
在做这个的时候,首先用stroyboard画出来界面UI,这个很简单,不多说了,然后下一步就是自定义xib做键盘上面的那一栏了,需要自己做xib还有view,详细代码下面会有,当我们点击的时候,我们的键盘的frame变化形成弹出的效果,这个在前面QQ案例里面有,其实就是通知的用法,其中我们自定义工具条的“上一个”“下一个”“完成”这些按钮,我们用了代理的方式来完成的。代码,注意点
代理
首先我们要自己写自己的代理:[code]@protocol NYKeyboardToolDelegate <NSObject> -(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType; @end
然后要在我们设置代理的类那让他实现我们的协议
[code]@interface NYViewController () <NYKeyboardToolDelegate>
创建工具栏 ,设置代理,把所有的UITextField遍历到放到全局变量_fields中来,设置属性
[code]//创建工具栏 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; } }
记住要实现协议方法否则工具条按钮没用
[code]#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变化动画,还有通知
初始化自定义键盘[code]//1:初始化自定义键盘 -(void)setupCustomKeyboard { UIDatePicker *datePicker = [[UIDatePicker alloc] init]; //设置地区 datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; //设置模式 datePicker.datePickerMode = UIDatePickerModeDate; //把datePicker放到birthday的键盘中 self.birthdayField.inputView = datePicker; }
键盘frame变化除了,开通通知
建立监听(通知)
[code]//建立监听 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
键盘frame的变化
[code]//键盘的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; }]; } }
其他小地方
关闭键盘[code]//开始触摸的方法 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //关闭键盘 [self.view endEditing:YES]; }
view中又很多UITextField 然后我们想知道哪一个打开了键盘,就是获取哪一个是响应者
[code]//获取当前 textField的响应者在_fields中的索引 //返回-1代表没有找到 -(int)getCurrentResponderIndex { //遍历所有的textField获取响应值 for (UITextField *tf in _fields) { if (tf.isFirstResponder) { return [_fields indexOfObject:tf]; } } return -1; }
UIDatePicker日期键盘的用法:
[code] UIDatePicker *datePicker = [[UIDatePicker alloc] init]; //设置地区 datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; //设置模式 datePicker.datePickerMode = UIDatePickerModeDate; //把datePicker放到birthday的键盘中 self.birthdayField.inputView = datePicker;
相关文章推荐
- AJ学IOS(23)UI之控制器管理
- AJ学IOS(20)UI之UIPickerView_点菜系统
- AJ学IOS(19)UI之QQ好友列表
- AJ学IOS(18)UI之QQ聊天布局_键盘通知实现自动弹出隐藏_自动回复
- AJ学IOS(17)UI之纯代码自定义Cell实现新浪微博UI
- AJ学IOS(16)UI之XIB自定义Cell实现团购UI
- AJ学IOS(15)UI之曾经大热的打砖块小游戏
- AJ学IOS(14)UI之UITableView扩充_表格的修改_(增删移动)
- AJ学IOS(13)UI之UITableView学习(下)汽车名牌带右侧索引
- AJ学IOS(12)UI之UITableView学习(上)LOL英雄联盟练习
- AJ学IOS(11)UI之图片自动轮播
- AJ学IOS(10)UI之_NSTimer_ios计时器
- AJ学IOS(09)UI之UIScrollView代理触摸实现_图片缩放
- AJ学IOS(08)UI之热门_喜马拉雅UI实现-UIScrollView的使用
- AJ学IOS(07)UI之UITextField代理事件_类似QQ登陆窗口的简单实现
- AJ学IOS(06)UI之iOS热门游戏_超级猜图
- AJ学IOS(05)UI之360等下载管理器九宫格UI
- AJ学IOS(04)UI之半小时搞定Tom猫
- AJ学IOS(03)UI之纯代码实现UI——图片查看器
- AJ学IOS(02)UI之按钮操作 点击变换 移动 放大缩小 旋转