iOS 键盘总结
来源:互联网 发布:工厂办公软件 编辑:程序博客网 时间:2024/06/07 10:18
- iOS 键盘
- 键盘类型
- 键盘 return 按钮类型
- 键盘外观
- 键盘 return 按钮点击事件
- 监听键盘弹出收回事件
- Notifications
- Keyboard Notification User Info Keys
- 实现一个键盘弹出的demo
iOS 键盘
iOS 键盘是苹果自带的系统视图,是一个Window。
键盘类型
typedef enum : NSInteger { UIKeyboardTypeDefault, UIKeyboardTypeASCIICapable, UIKeyboardTypeNumbersAndPunctuation, UIKeyboardTypeURL, UIKeyboardTypeNumberPad, UIKeyboardTypePhonePad, UIKeyboardTypeNamePhonePad, UIKeyboardTypeEmailAddress, UIKeyboardTypeDecimalPad, UIKeyboardTypeTwitter, UIKeyboardTypeWebSearch, UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable } UIKeyboardType;
各种类型的图像:
UIKeyboardTypeASCIICapable
UIKeyboardTypeNumbersAndPunctuation
UIKeyboardTypeURL
UIKeyboardTypeNumberPad
UIKeyboardTypePhonePad
UIKeyboardTypeNamePhonePad
UIKeyboardTypeEmailAddress
UIKeyboardTypeDecimalPad
UIKeyboardTypeTwitter
UIKeyboardTypeWebSearch
键盘 return 按钮类型
typedef enum : NSInteger { UIReturnKeyDefault, UIReturnKeyGo, UIReturnKeyGoogle, UIReturnKeyJoin, UIReturnKeyNext, UIReturnKeyRoute, UIReturnKeySearch, UIReturnKeySend, UIReturnKeyYahoo, UIReturnKeyDone, UIReturnKeyEmergencyCall, UIReturnKeyContinue,} UIReturnKeyType;
键盘外观
typedef enum : NSInteger { UIKeyboardAppearanceDefault, UIKeyboardAppearanceDark, UIKeyboardAppearanceLight, UIKeyboardAppearanceAlert,} UIKeyboardAppearance;
键盘 return 按钮点击事件
在 UITextField
的代理方法如下:
- (BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; return true; }
在 UITextView
的代理中的响应方法如下:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{ if ([text isEqualToString:@"\n"]){ //判断输入的字是否是回车,即按下return //在这里做你响应return键的代码 return NO; //这里返回NO,就代表return键值失效,即页面上按下return,不会出现换行,如果为yes,则输入页面会换行 } return YES;}
监听键盘弹出收回事件
Notifications
观察键盘弹出之前的状态,object 为 nil。
UIKeyboardWillShowNotification观察键盘弹出之后的状态
UIKeyboardDidShowNotification观察键盘消失之前的状态
UIKeyboardWillHideNotification观察键盘消失之后的状态
UIKeyboardDidHideNotification观察键盘 frame 改变之前的状态
UIKeyboardWillChangeFrameNotification观察键盘 frame 改变之后的状态
UIKeyboardDidChangeFrameNotification
Keyboard Notification User Info Keys
获得键盘开始弹出位置的坐标,返回的是NSValue对象
NSString *const UIKeyboardFrameBeginUserInfoKey;获得键盘弹出结束之后的坐标,返回的是NSValue对象
NSString *const UIKeyboardFrameEndUserInfoKey;获得键盘弹出或者收回的动画方式( UIViewAnimationCurve 常量),返回NSNumber对象
NSString *const UIKeyboardAnimationCurveUserInfoKey;获得动画持续的时间(单位s),返回NSNumber对象
NSString *const UIKeyboardAnimationDurationUserInfoKey;获得一个Bool值判断当前的键盘是不是当前应用的,返回NSNumber对象
NSString *const UIKeyboardIsLocalUserInfoKey;
实现一个键盘弹出的demo
实现效果如图:
代码片段如下:
- (void)viewDidLoad { [super viewDidLoad]; // 对屏幕添加手势 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)]; [self.view addGestureRecognizer:tap]; // 添加观察者 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardDidShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardWillHideNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardDidHideNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardWillChangeFrameNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyBoardAction:) name:UIKeyboardDidChangeFrameNotification object:nil];}- (void)keyBoardAction:(NSNotification *)notifation { if ([notifation.name isEqual:UIKeyboardWillShowNotification]) { NSLog(@"键盘将要弹出"); // 获取键盘的各种信息 CGRect startFrame = [[notifation.userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey]CGRectValue]; CGRect endFrame = [[notifation.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue]; NSNumber *animationCurve = [notifation.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey]; double animationTimes = [[notifation.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]; BOOL isCurrentViewKeyBoard = [[notifation.userInfo objectForKey:UIKeyboardIsLocalUserInfoKey]boolValue]; NSLog(@"startFrame = %@, endFrame = %@, animationCurve = %@, animationTimes = %f, isCurrentViewKeyBoard = %d", [NSValue valueWithCGRect:startFrame] , [NSValue valueWithCGRect:endFrame], animationCurve, animationTimes, (isCurrentViewKeyBoard == 1)? 1 : 0); CGFloat hight = startFrame.size.height; [UIView animateWithDuration:animationTimes animations:^{ self.sendTextView.transform = CGAffineTransformMakeTranslation(0, -hight); }]; } if ([notifation.name isEqual:UIKeyboardDidShowNotification]) { NSLog(@"键盘已经弹出"); NSLog(@"self.sendTextView.frame = %@", [NSValue valueWithCGRect:self.sendTextView.frame]); } if ([notifation.name isEqual:UIKeyboardWillHideNotification]) { NSLog(@"键盘将要收回"); double animationTimes = [[notifation.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey]doubleValue]; [UIView animateWithDuration:animationTimes animations:^{ self.sendTextView.transform = CGAffineTransformMakeTranslation(0, 0); }]; } if ([notifation.name isEqual:UIKeyboardDidHideNotification]) { NSLog(@"键盘已经收回"); } if ([notifation.name isEqual:UIKeyboardWillChangeFrameNotification]) { NSLog(@"键盘将要改变Frame"); } if ([notifation.name isEqual:UIKeyboardDidChangeFrameNotification]) { NSLog(@"键盘已经改变frame"); }}- (void)tapAction:(id)sender { [self.tF resignFirstResponder];}
在一个键盘弹出和收回的过程中观察者的先后执行如图所示:
- ios隐藏键盘总结
- iOS键盘总结
- iOS 键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- iOS开发- 隐藏键盘总结
- IOS键盘触摸退出总结
- iOS开发隐藏键盘方法总结
- iOS开发隐藏键盘方法总结
- iOS开发隐藏键盘方法总结
- iOS开发隐藏键盘方法总结
- iOS开发隐藏键盘方法总结
- 关于python协程与异步举例
- Android开发艺术探索(九)
- PUTTY+Xmanager登录centos,实现ncl绘图本地图形化显示
- C语言,有一个已排好的数组,要求输入一个数组后,按原来的排序规律将它插入数组
- msvcprtd.lib(MSVCP90D.dll) : error LNK2005
- iOS 键盘总结
- 【爬虫】爬https网站的报错问题
- js实现input元素输入图片即时预览
- ios 关于页面间的跳转
- 2016.9.1 开始新工作 记录学习与工作 济南
- Python 数据结构与算法 —— 哈弗曼树
- HiveQL详解
- 如何将json对象转化为字符串?
- POJ 3694 Network (边双连通缩点、LCA)