ios 利用通知处理键盘的显示与隐藏

1.监听键盘通知

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    

    // 监听键盘的通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];

    

}


 当键盘改变了 frame( 位置和尺寸 ) 的时候调用

- (void)keyboardWillChangeFrame:(NSNotification *)note

{

    // 设置窗口的颜色

    self.view.window.backgroundColor = self.tableView.backgroundColor;

    

    // 0.取出键盘动画的时间

    CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    

    // 1.取得键盘最后的frame

    CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];

    

    // 2.计算控制器的view需要平移的距离

    CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;

    

    // 3.执行动画

    [UIView animateWithDuration:duration animations:^{

        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);

    }];

}


 2. 当开始拖拽表格的时候调用(隐藏键盘)

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView

{

    // 退出键盘

    [self.view endEditing:YES];

}

3.注销监听器

- (void)dealloc

{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值