iOS UI基础__键盘keyboard的监听和消失

释放双眼,带上耳机,听听看~!

1.键盘的消失方式

////界面滚动的时候,键盘退出
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
[self.view endEditing:YES];
}
//拖拽界面的时候,键盘退出
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[self.view endEditing:YES];
}

2.注册键盘监听

//注册键盘监听
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
//注册监听键盘自动发出的UIKeyboardWillChangeFrameNotification通知,调用self的keyboardFrameChange:进行处理
[center addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
//键盘监听处理方法
- (void) keyboardFrameChange:(NSNotification *)notice {
//NSLog(@"%@",notice.userInfo);//可以打印出各种信息
//1.获取动画时间
CGFloat time=[notice.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat endY=[notice.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue].origin.y;
//获取偏移值
CGFloat offset=endY-self.view.bounds.size.height;
[UIView animateWithDuration:time animations:^{
self.view.transform = CGAffineTransformMakeTranslation(0, offset);
}];
}
//移除监听
- (void) dealloc {
NSNotificationCenter *center=[NSNotificationCenter defaultCenter];
[center removeObserver:self];
}

3.文本框的代理方法

//当键盘点击sender按钮后调用该方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
return  YES;
}

人已赞赏
iOS文章

iOS 长按手势响应执行两次解决办法

2020-2-20 16:57:08

iOS文章

iOS 防QQ好友列表制作

2020-2-20 17:20:56

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索