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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值