键盘处理
来源:互联网 发布:来深圳做销售知乎 编辑:程序博客网 时间:2024/05/22 00:51
键盘处理——使用
Mapwindow会发送通知,我们接收通知就可以了
1.布局:
2.拉成属性(私有):
@property(weak,nonatomic)IBOutletUIView*inputView;
3.实现
//程序显示的时候调用
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//先拿到通知中心
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
//收听通知
- (void)viewWillAppear:(BOOL)animated
{
[superviewWillAppear:animated];
//先拿到通知中心
NSNotificationCenter *center = [NSNotificationCenterdefaultCenter];
//收听通知
//UIKeyboardWillShowNotification键盘将要显示的一个通知
//UIKeyboardWillShowNotification;<-键盘将要显示
//UIKeyboardDidShowNotification;<-键盘已经显示
//UIKeyboardWillHideNotification;<-键盘将要隐藏
//UIKeyboardDidHideNotification;<-键盘已经隐藏
//监听键盘显示
[center addObserver:selfselector:@selector(keyboardAppear:)name:UIKeyboardWillShowNotificationobject:nil];
//监听键盘隐藏
[center addObserver:selfselector:@selector(keyboardDisappear:)name:UIKeyboardWillHideNotificationobject:nil];
[center addObserver:selfselector:@selector(keyboardAppear:)name:UIKeyboardWillShowNotificationobject:nil];
//监听键盘隐藏
[center addObserver:selfselector:@selector(keyboardDisappear:)name:UIKeyboardWillHideNotificationobject:nil];
}
//释放后调用
- (void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
//删除
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
- (void)viewWillDisappear:(BOOL)animated
{
[superviewWillDisappear:animated];
//删除
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
[[NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
}
//UIKeyboardFrameBeginUserInfoKey<-键盘开始(弹起)的时候有多大
//UIKeyboardFrameEndUserInfoKey<-键盘结束(隐藏)的时候有多大
//UIKeyboardAnimationDurationUserInfoKey<-键盘弹起的时候动画有多长
//键盘显示(弹起)的方法
- (void)keyboardAppear:(NSNotification*)notification
{
//1.获取键盘的高度
//生成字典
- (void)keyboardAppear:(NSNotification*)notification
{
//1.获取键盘的高度
//生成字典
NSDictionary *userInfo = notification.userInfo;
NSLog(@"%@",userInfo);//此处输出下面有截图
//拿到动画结束之后的frame CGRect不能放到NSDictionary里,所以是NSValue
NSValue *value = userInfo[UIKeyboardFrameEndUserInfoKey];
//拿到frame
CGRect keyboardFrame = [valueCGRectValue];
//2.计算输入框的frame
CGRect inputFrame =self.inputView.frame;
//设置键盘弹起后inputView的位置
//视图的高-键盘的高-inputView的高
inputFrame.origin.y=self.view.bounds.size.height- keyboardFrame.size.height- inputFrame.size.height;
//3.设置动画
NSValue *value = userInfo[UIKeyboardFrameEndUserInfoKey];
//拿到frame
CGRect keyboardFrame = [valueCGRectValue];
//2.计算输入框的frame
CGRect inputFrame =self.inputView.frame;
//设置键盘弹起后inputView的位置
//视图的高-键盘的高-inputView的高
inputFrame.origin.y=self.view.bounds.size.height- keyboardFrame.size.height- inputFrame.size.height;
//3.设置动画
//动画的时间[userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue]<-键盘动画时间[转成doubleValue]
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
//动画是先快后慢还是先快后慢
UIViewAnimationOptions options = [userInfo[UIKeyboardAnimationCurveUserInfoKey]intValue];
[UIViewanimateWithDuration:durationdelay:0 options:optionsanimations:^{
self.inputView.frame= inputFrame;
//动画是先快后慢还是先快后慢
UIViewAnimationOptions options = [userInfo[UIKeyboardAnimationCurveUserInfoKey]intValue];
[UIViewanimateWithDuration:durationdelay:0 options:optionsanimations:^{
self.inputView.frame= inputFrame;
} completion:nil];
}
//键盘隐藏的方法
- (void)keyboardDisappear:(NSNotification*)notification
{
//拿到frame
CGRect frame = self.inputView.frame;
//修改y
frame.origin.y=self.view.bounds.size.height- frame.size.height;
NSDictionary *userInfo = notification.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationOptions options = [userInfo[UIKeyboardAnimationCurveUserInfoKey]intValue];
[UIViewanimateWithDuration:durationdelay:0 options:optionsanimations:^{
self.inputView.frame= frame;
} completion:nil];
- (void)keyboardDisappear:(NSNotification*)notification
{
//拿到frame
CGRect frame = self.inputView.frame;
//修改y
frame.origin.y=self.view.bounds.size.height- frame.size.height;
NSDictionary *userInfo = notification.userInfo;
NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
UIViewAnimationOptions options = [userInfo[UIKeyboardAnimationCurveUserInfoKey]intValue];
[UIViewanimateWithDuration:durationdelay:0 options:optionsanimations:^{
self.inputView.frame= frame;
} completion:nil];
}
//拉拽
- (IBAction)inputCompletion:(UITextField*)sender
{
//释放
{
//释放
[sender resignFirstResponder];
}
此时,键盘就能实现弹起和隐藏了并且inputView会随着键盘的弹起而变化
键盘缩回去:
在上面的代码基础上修改
声明属性(私有):
@property(strong,nonatomic)NSMutableArray*messages;
@property(strong,nonatomic)UITableView*tableView;
实现:
- (NSMutableArray*)messages
{
if(!_messages)_messages= [[NSMutableArrayalloc]init];
return _messages;
{
if(!_messages)_messages= [[NSMutableArrayalloc]init];
return _messages;
}
- (IBAction)inputCompletion:(UITextField*)sender
{
//把输入的信息加入到messages中
[self.messagesaddObject:sender.text];
//刷新数据
[self.tableViewreloadData];
}
[self.messagesaddObject:sender.text];
//刷新数据
[self.tableViewreloadData];
}
//纯代码创建一个tableView别忘了遵守协议<UITableViewDataSource>
staticNSString*cellIdentifier =@"MessageCell";
- (void)viewDidLoad
{
[superviewDidLoad];
UITableView *tableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
self.tableView= tableView;
tableView.dataSource=self;
//注册cell
[tableView registerClass:[UITableViewCellclass]forCellReuseIdentifier:cellIdentifier];
//[self.view addSubview:tableView];
//因为手动创建tableView在storyboard之后创建的,storyboard会被tableView
[self.viewinsertSubview:tableViewatIndex:0];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return self.messages.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifierforIndexPath:indexPath];
cell.textLabel.text=self.messages[indexPath.row];
return cell;
}
- (void)viewDidLoad
{
[superviewDidLoad];
UITableView *tableView = [[UITableViewalloc]initWithFrame:self.view.framestyle:UITableViewStylePlain];
self.tableView= tableView;
tableView.dataSource=self;
//注册cell
[tableView registerClass:[UITableViewCellclass]forCellReuseIdentifier:cellIdentifier];
//[self.view addSubview:tableView];
//因为手动创建tableView在storyboard之后创建的,storyboard会被tableView
[self.viewinsertSubview:tableViewatIndex:0];
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{
return self.messages.count;
}
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellIdentifierforIndexPath:indexPath];
cell.textLabel.text=self.messages[indexPath.row];
return cell;
}
0 0
- 键盘处理
- 键盘处理
- 键盘处理
- swing键盘事件处理
- 键盘事件处理
- 处理键盘事件
- js处理键盘事件
- VC处理键盘消息
- 处理键盘事件
- 键盘消息的处理
- PreTranslateMessage 处理键盘消息
- UITextField 键盘处理
- VC处理键盘消息
- IOS键盘处理
- PreTranslateMessage处理键盘消息
- 键盘消息处理
- Windows键盘消息处理
- JS处理键盘事件
- 安装MongoDB和简单使用
- Canvas学习--绘制五角星(一)
- 【深入浅出IOS开发】创建Model类型Controller
- 循环链表c++实现
- 常用正则表达式
- 键盘处理
- 通知(Notification)
- 高级js代码
- Python中的string模块详解——string的中文API文档
- 搜索框
- ioss 8.1 to 8.2 api differences
- Otto源码分析
- Java中泛型二分查找,继承Comparable接口
- 几种常用的JS类定义方法