iOS 键盘的弹出与收起

UIKeyboardWillShowNotification//弹出

UIKeyboardWillHideNotification//收起

 

所以需要监听这两个 最一些操作 比如 视图的移动

 

//注册观察键盘的变化
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didClickKeyboard:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didKboardDisappear:) name:UIKeyboardWillHideNotification object:nil];

方法实现

一般的:

#pragma mark - 键盘即将跳出
-(void)didClickKeyboard:(NSNotification *)sender{

    CGFloat durition = [sender.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    CGRect keyboardRect = [sender.userInfo[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    CGFloat keyboardHeight = keyboardRect.size.height;
    [UIView animateWithDuration:durition animations:^{
        
        self.view.transform = CGAffineTransformMakeTranslation(0, -keyboardHeight);
    }];
}
#pragma mark - 当键盘即将消失
-(void)didKboardDisappear:(NSNotification *)sender{
    
    CGFloat duration = [sender.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    [UIView animateWithDuration:duration animations:^{
        
        self.view.transform = CGAffineTransformIdentity;
    }];
}

 

如果视图在scrollview上边:

#pragma mark - 键盘即将跳出
-(void)didClickKeyboard:(NSNotification *)sender{

    [UIView animateWithDuration:0.1 animations:^{
        [self.scrollView setContentOffset:CGPointMake(0,30) animated:YES];
    }];
    
}
#pragma mark - 当键盘即将消失
-(void)didKboardDisappear:(NSNotification *)sender{
    
    [UIView animateWithDuration:0.1 animations:^{
        [self.scrollView setContentOffset:CGPointMake(0,0) animated:YES];
    }];
    
}

 

转载于:https://my.oschina.net/rainwz/blog/1941684

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值