首页 技术 正文
技术 2022年11月8日
0 收藏 899 点赞 1,837 浏览 5039 个字

*****HMViewController.m

#import "HMViewController.h"
#import "HMKeyboardTool.h"@interface HMViewController ()<HMKeyboardToolDelegate>{ NSArray *_fields;//存储所有的textField
}
@property (weak, nonatomic) IBOutlet UITextField *birthdayField;
@property (weak, nonatomic) IBOutlet UIView *inputContainer;//输入框容器view@end@implementation HMViewController- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //1.初始化自定议键盘
[self setupCustomKeyboard]; //2.设置每一个textfield的键盘工具view(inputAccessoryView) [self setupKeyboardTool]; //3.监听键盘的事件
[self setupKeyoardNotification]; NSLog(@"%@",_fields);
}//1.初始化自定议键盘
-(void)setupCustomKeyboard{
UIDatePicker *datePicker = [[UIDatePicker alloc] init];
datePicker.locale = [NSLocale localeWithLocaleIdentifier:@"zh"];
datePicker.datePickerMode = UIDatePickerModeDate; self.birthdayField.inputView = datePicker;
}//设置每一个textfield的键盘工具view(inputAccessoryView)
-(void)setupKeyboardTool{ //创建工具栏
HMKeyboardTool *tool = [HMKeyboardTool keyboardTool]; //设置代理
tool.delgate = self; //1.获取输入框窗口所有子控件
NSArray *views = self.inputContainer.subviews; //创建一个数据存储textfield
NSMutableArray *fieldsM = [NSMutableArray array]; //2.遍历
for (UIView *child in views) {
//如果子控制是UITextField的时候,设置inputAccessoryView
if ([child isKindOfClass:[UITextField class]]) {
UITextField *tf = (UITextField *)child;
tf.inputAccessoryView = tool;
//把textfield添加到数组
[fieldsM addObject:tf];
}
} _fields = fieldsM;}-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}//3.监听键盘的事件
-(void)setupKeyoardNotification{ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(kbFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];}//键盘的frame的变化-(void)kbFrameChange:(NSNotification *)nofifi{
//NSLog(@"%@",nofifi); //获取键盘改变化的y值
//这键盘结束时的frm
CGRect kbEndFrm =[nofifi.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; //键盘结束时的y
CGFloat kbEndY = kbEndFrm.origin.y; //获取当前的响应者
int currentIndex = [self getCurrentResponderIndex];
UITextField *currentTf = _fields[currentIndex];
CGFloat tfMaxY = CGRectGetMaxY(currentTf.frame) + self.inputContainer.frame.origin.y; NSLog(@"%f %f",kbEndY,tfMaxY);
//改变控制器view的transform //如果textfield的最大值在于键盘的y坐,才要往上移
if (tfMaxY > kbEndY) {
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformMakeTranslation(, kbEndY - tfMaxY);
}];
}else{
[UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}}#pragma mark 键盘工具条的代理
-(void)keyboardTool:(HMKeyboardTool *)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];
}
}//获取当前textfield的响应者索引
//如果返回-1代理没有找响应者
-(int)getCurrentResponderIndex{
//遍历所有的textfield获取响应者
for (UITextField *tf in _fields) {
if (tf.isFirstResponder) {
return [_fields indexOfObject:tf];
}
} return -;
}//让上一个field成功响应者
-(void)showProviousField:(int)currentIndex{
int proviousIndex = currentIndex - ; if (proviousIndex > ) {
UITextField *proviousTf = [_fields objectAtIndex:proviousIndex];
[proviousTf becomeFirstResponder];
}}//让下一个field成功响应者
-(void)showNextField:(int)currentIndex{
int nextIndex = currentIndex + ; //下一个索引不能超过_fields数组的个数
if (nextIndex < _fields.count) {
//让当前响应者分发去
UITextField *currentTf = [_fields objectAtIndex:currentIndex];
[currentTf resignFirstResponder]; UITextField *nextTf = [_fields objectAtIndex:nextIndex];
[nextTf becomeFirstResponder];
}}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES]; [UIView animateWithDuration:0.25 animations:^{
self.view.transform = CGAffineTransformIdentity;
}];
}
@end

****自定义view,HMKeyboardTool.h

#import <UIKit/UIKit.h>typedef enum {
KeyboardItemTypePrevious,//上一个
KeyboardItemTypeNext,//下一个
KeyboardItemTypeDone//完成
}KeyboardItemType;@class HMKeyboardTool;@protocol HMKeyboardToolDelegate <NSObject>-(void)keyboardTool:(HMKeyboardTool *)keyboardTool didClickItemType:(KeyboardItemType)itemType;@end@interface HMKeyboardTool : UIView//添加代理
@property(nonatomic,weak)id<HMKeyboardToolDelegate> delgate;+(instancetype)keyboardTool;@end

****自定义HMKeyboardTool.m

#import "HMKeyboardTool.h"@interface HMKeyboardTool()- (IBAction)previous:(id)sender;- (IBAction)next:(id)sender;
- (IBAction)done:(id)sender;@end@implementation HMKeyboardTool+(instancetype)keyboardTool{
return [[[NSBundle mainBundle] loadNibNamed:@"HMKeyboardTool" owner:nil options:nil] lastObject];
}//上一个
- (IBAction)previous:(id)sender { //判断代理有没有实现相应的方法
if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delgate keyboardTool:self didClickItemType:KeyboardItemTypePrevious];
}
}//下一个
- (IBAction)next:(id)sender {
//判断代理有没有实现相应的方法
if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeNext];
}}//完成
- (IBAction)done:(id)sender { //判断代理有没有实现相应的方法
if ([self.delgate respondsToSelector:@selector(keyboardTool:didClickItemType:)]) {
[self.delgate keyboardTool:self didClickItemType:KeyboardItemTypeDone];
}
}
@end
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,990
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,504
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,348
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,133
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,765
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,843