键盘通知

1.什么是通知?

系统帮助两个对象完成消息传递的一种机制.与代理模式的发消息的区别是:代理模式中A对象明确持有B对象的引用,然后A只能给B对象发消息,但是在通知中,引入了一个第三方,叫做通知中心的角色,A对象并不知道要给谁发消息,而是把消息交给通知中心,由通知中心将消息发给订阅了消息的B对象或者是更多地订阅了消息的对象

 

2.弹起键盘

    //增加键盘的弹起通知监听

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(openKeyboard:) name:UIKeyboardWillShowNotification object:nil];

// 有键盘弹起,此方法就会被自动执行

-(void)openKeyboard:(NSNotification *)noti

{

    // 获取键盘的 frame 数据

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

    // 获取键盘动画的种类

    int options = [noti.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];

    // 获取键盘动画的时长

    NSTimeInterval duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

    // 修改文本框底部的约束的值

    self.bottomConstraint.constant = keyboardFrame.size.height;

    // 在动画内调用 layoutIfNeeded 方法

    [UIView animateWithDuration:duration delay:0 options:options animations:^{

        [self.view layoutIfNeeded];

    } completion:nil];

}

 

3.收起键盘

   //增加键盘的收起通知监听

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(closeKeyboard:) name:UIKeyboardWillHideNotification object:nil];

-(void)closeKeyboard:(NSNotification *)noti

{

    // 获取键盘动画的种类

    int options = [noti.userInfo[UIKeyboardAnimationCurveUserInfoKey] intValue];

    // 获取键盘动画的时长

    NSTimeInterval duration = [noti.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];

}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值