1 // 2 // LXKeyboardTool.h 3 // 注册界面 4 // 5 // Created by 刘羽 on 16/1/5. 6 // Copyright © 2016年 LX. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 typedef enum{ 12 keyboardItemTypeNext, 13 keyboardItemTypePrevious, 14 keyboardItemTypeDone 15 16 }keyboardItemType; 17 18 @class LXKeyboardTool; 19 20 @protocol LXKeyboardToolDelegate<NSObject> 21 22 -(void)keyboardTool:(LXKeyboardTool *)keyboardTool didClickItemType:(keyboardItemType)itemType; 23 24 @end 25 26 @interface LXKeyboardTool : UIView 27 //添加代理 28 @property(nonatomic,weak)id<LXKeyboardToolDelegate>delegata; 29 +(instancetype)keyboardTool; 30 31 @end
1 // 2 // LXKeyboardTool.m 3 // 注册界面 4 // 5 // Created by 刘羽 on 16/1/5. 6 // Copyright © 2016年 LX. All rights reserved. 7 // 8 9 #import "LXKeyboardTool.h" 10 @interface LXKeyboardTool() 11 12 - (IBAction)previous:(id)sender; 13 14 - (IBAction)next:(id)sender; 15 - (IBAction)done:(id)sender; 16 17 @end 18 19 @implementation LXKeyboardTool 20 21 /* 22 // Only override drawRect: if you perform custom drawing. 23 // An empty implementation adversely affects performance during animation. 24 - (void)drawRect:(CGRect)rect { 25 // Drawing code 26 } 27 */ 28 29 +(instancetype)keyboardTool 30 { 31 return [[[NSBundle mainBundle] loadNibNamed:@"LXKeyboardTool" owner:nil options:nil] lastObject]; 32 } 33 34 -(void)previous:(id)sender 35 {//判断代理有没有实现相应的方法 36 if ([self.delegata respondsToSelector:@selector(keyboardTool:didClickItemType:)]) { 37 [self.delegata keyboardTool:self didClickItemType:keyboardItemTypePrevious]; 38 } 39 } 40 -(void)next:(id)sender 41 { 42 if ([self.delegata respondsToSelector:@selector(keyboardTool:didClickItemType:)]) { 43 [self.delegata keyboardTool:self didClickItemType:keyboardItemTypeNext]; 44 } 45 } 46 -(void)done:(id)sender 47 { 48 if ([self.delegata respondsToSelector:@selector(keyboardTool:didClickItemType:)]) { 49 [self.delegata keyboardTool:self didClickItemType:keyboardItemTypeDone]; 50 } 51 } 52 53 @end
// // ViewController.m // 注册界面 // // Created by 刘羽 on 16/1/5. // Copyright © 2016年 LX. All rights reserved. // #import "ViewController.h" #import "LXKeyboardTool.h" @interface ViewController ()<LXKeyboardToolDelegate>{ NSArray *_fileds;//存储所有的textFields } @property (weak, nonatomic) IBOutlet UIView *inputContainer; @property (weak, nonatomic) IBOutlet UITextField *birthdayField; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //1.初始化自定义键盘 [self setupCustomKeyboard]; //2.设置每一个textFiled键盘工具view(inputAccessoryView) [self setupKeyboardTool]; //3监听键盘的事件 [self setupKeyboardNotification]; } //1初始化自定义键盘 -(void)setupCustomKeyboard { UIDatePicker *datePicker = [[UIDatePicker alloc]init]; datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"]; datePicker.datePickerMode = UIDatePickerModeDate; self.birthdayField.inputView = datePicker; } //2.设置每一个textFiled键盘工具view(inputAccessoryView) -(void)setupKeyboardTool { //创建工具栏 LXKeyboardTool *tool = [LXKeyboardTool keyboardTool]; //设置代理 tool.delegata = self; //1、获取所有输入框窗口的所有子控件 NSArray *views = self.inputContainer.subviews; //2、创建一个可变数组 NSMutableArray *arrayM = [NSMutableArray array]; //3、如果子控件是UITextView 则设置inputAccessView for (UIView *child in views) { if ([child isKindOfClass:[UITextField class]]) { UITextField *uf = (UITextField *)child; uf.inputAccessoryView = tool; [arrayM addObject:uf]; } } _fileds = arrayM; } -(void)dealloc { //移除监听 [[NSNotificationCenter defaultCenter] removeObserver:self]; } //监听键盘的事件 -(void)setupKeyboardNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil]; } //键盘frame的变化 -(void)kbFrameChange:(NSNotification *)notification { //获取键盘改变时的frame,并转换成CGrect类型 CGRect kbEndFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; //获取键盘改变时的y值 CGFloat kbEndY = kbEndFrame.origin.y; //获取当前的响应者 int currentIndex = [self getCurrenResponderIndex]; UITextField *tf = _fileds[currentIndex]; CGFloat tfMaxY = CGRectGetMaxY(tf.frame) + self.inputContainer.frame.origin.y; //改变控制器view的transform //如果textField的最大y值大于键盘的y值,才往上移动 if (tfMaxY > kbEndY) { [UIView animateWithDuration:0.25 animations:^{ self.view.transform = CGAffineTransformMakeTranslation(0, tfMaxY - kbEndY); }]; }else { [UIView animateWithDuration:0.25 animations:^{ self.view.transform = CGAffineTransformIdentity; }]; } } #pragma mark 键盘工具条的代理 -(void)keyboardTool:(LXKeyboardTool *)keyboardTool didClickItemType:(keyboardItemType)itemType { // 获取当前响应者的索引 int currentIndex = [self getCurrenResponderIndex]; switch (itemType) { case keyboardItemTypePrevious: [self showPreviousField:currentIndex]; break; case keyboardItemTypeNext : [self showNextField:currentIndex]; break; case keyboardItemTypeDone: [self touchesBegan:nil withEvent:nil]; } } //获取当前textField响应者的索引 -(int)getCurrenResponderIndex { for (UITextField *tf in _fileds) { if (tf.isFirstResponder) {//如果是第一响应者 return [_fileds indexOfObject:tf]; } } return -1;//没有找到就返回-1 } //让上一个FiledText成为响应者 -(void)showPreviousField:(int)currentIndex { int previousIndex = currentIndex - 1; if (previousIndex > -1) { UITextField *preTextField = [_fileds objectAtIndex:previousIndex]; [preTextField becomeFirstResponder]; } } //让下一个成为响应者 -(void)showNextField:(int)currentIndex { int NextIndex = currentIndex + 1; if (NextIndex < _fileds.count) { UITextField *nextTextField = [_fileds objectAtIndex:NextIndex]; [nextTextField becomeFirstResponder]; } } -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; [UIView animateWithDuration:0.25 animations:^{ self.view.transform = CGAffineTransformIdentity; }]; } @end
原文:http://www.cnblogs.com/lxzju/p/5103407.html