猫猫分享,必须精品
素材代码地址:http://download.csdn.net/detail/u013357243/8614731
原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
先看效果
代码
NYViewController.m
//
// NYViewController.m
// 注册
//
// Created by apple on 15-4-19.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYViewController.h"
#import "NYKeyboardTool.h"
@interface NYViewController () <NYKeyboardToolDelegate>{
NSArray *_fields;//储存所有的TextField
}
//输入框容器view
@property (weak, nonatomic) IBOutlet UIView *inputContainer;
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
@end
@implementation NYViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//1:初始化自定义键盘
[self setupCustomKeyboard];
//2:设置每一个textfield的键盘工具栏(inputAccessoryView)
[self setupKeyboardTool];
//3:监听键盘的事件
[self setupKeyboardNotification];
}
//1:初始化自定义键盘
-(void)setupCustomKeyboard
{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
//设置地区
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
//设置模式
datePicker.datePickerMode = UIDatePickerModeDate;
//把datePicker放到birthday的键盘中
self.birthdayField.inputView = datePicker;
}
//2:设置每一个textfield的键盘工具栏(inputAccessoryView)
-(void)setupKeyboardTool
{
//创建工具栏
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;
}
}
}
//3:监听键盘的事件
-(void)setupKeyboardNotification
{
//建立监听
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
}
//键盘的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;
}];
}
}
//获取当前 textField的响应者在_fields中的索引
//返回-1代表没有找到
-(int)getCurrentResponderIndex
{
//遍历所有的textField获取响应值
for (UITextField *tf in _fields) {
if (tf.isFirstResponder) {
return [_fields indexOfObject:tf];
}
}
return -1;
}
//开始触摸的方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//关闭键盘
[self.view endEditing:YES];
}
#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];
}
}
@end
自定义工具条代码
NYKeyboardTool.h
//
// NYKeyboardTool.h
// 注册
//
// Created by apple on 15-4-19.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import <UIKit/UIKit.h>
@class NYKeyboardTool;
typedef enum {
KeyboardItemTypePrevious,//上一个
KeyboardItemTypeNext,//下一个
KeyboardItemTypeDone//完成
}KeyboardItemType;
@protocol NYKeyboardToolDelegate <NSObject>
-(void)keyboardTool:(NYKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;
@end
@interface NYKeyboardTool : UITableViewHeaderFooterView
//添加代理
@property (nonatomic, weak) id<NYKeyboardToolDelegate> delegate;
//创建对象
+(instancetype)keyBoardTool;
@end
NYKeyboardTool.m
//
// NYKeyboardTool.m
// 注册
//
// Created by apple on 15-4-19.
// Copyright (c) 2015年 znycat. All rights reserved.
//
#import "NYKeyboardTool.h"
@interface NYKeyboardTool()
/**上一个*/
- (IBAction)previous:(id)sender;
/**下一个*/
- (IBAction)next:(id)sender;
/**完成*/
- (IBAction)done:(id)sender;
@end
@implementation NYKeyboardTool
//创建对象
+(instancetype)keyBoardTool
{
return [[[NSBundle mainBundle]loadNibNamed:@"NYKeyboardTool" owner:nil options:nil]lastObject];
}
- (IBAction)previous:(id)sender {
//判断代理有没有实现相应的方法。
if ([self.delegate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delegate keyboardTool:self didClickItemType:KeyboardItemTypePrevious];
}
}
- (IBAction)next:(id)sender {
//判断代理有没有实现相应的方法。
if ([self.delegate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delegate keyboardTool:self didClickItemType:KeyboardItemTypeNext];
}
}
- (IBAction)done:(id)sender {
//判断代理有没有实现相应的方法。
if ([self.delegate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delegate keyboardTool:self didClickItemType:KeyboardItemTypeDone];
}
}
@end
ps:新建iOS交流学习群:304570962 可以加猫猫QQ:1764541256 或则微信znycat 让我们一起努力学习吧。
翟乃玉的博客
地址:http://blog.csdn.net/u013357243?viewmode=contents
时间: 2024-10-08 05:28:07