键盘处理

来源:互联网 发布:来深圳做销售知乎 编辑:程序博客网 时间:2024/05/22 00:51

键盘处理——使用
Map

window会发送通知,我们接收通知就可以了
1.布局:
2.拉成属性(私有):
@property(weak,nonatomic)IBOutletUIView*inputView;
3.实现
//程序显示的时候调用
- (
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];
}

//释放后调用
- (
void)viewWillDisappear:(BOOL)animated
{
    [
superviewWillDisappear:animated];
   
//删除
    [[
NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillShowNotificationobject:nil];
    [[
NSNotificationCenterdefaultCenter]removeObserver:selfname:UIKeyboardWillHideNotificationobject:nil];
}

//UIKeyboardFrameBeginUserInfoKey<-键盘开始(弹起)的时候有多大        
//UIKeyboardFrameEndUserInfoKey<-键盘结束(隐藏)的时候有多大         
//UIKeyboardAnimationDurationUserInfoKey<-键盘弹起的时候动画有多长

//键盘显示(弹起)的方法
- (
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.设置动画
    //动画的时间[userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue]<-键盘动画时间[转成doubleValue] 
    NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey]doubleValue];
   
//动画是先快后慢还是先快后慢
   
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];
}

//拉拽
- (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;
}

- (
IBAction)inputCompletion:(UITextField*)sender
{
    //把输入的信息加入到messages
    [
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];
   
//因为手动创建tableViewstoryboard之后创建的,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
原创粉丝点击