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

来源:互联网 发布:菜刀 知乎 编辑:程序博客网 时间:2024/05/17 03:38

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;}
0 0
原创粉丝点击