iOS 键盘监听与视图滚动

//通过设置该监听,用于获取键盘弹出时的轨迹属性及键盘frame
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//通过设置该监听,用于获取键盘关闭时的轨迹属性及键盘frame
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
#pragma mark - 键盘事件监听
- (void)keyboardWillShow:(NSNotification *)notice {
    UIViewAnimationCurve _animationCurve;
    CGFloat _animationDuration;
    NSDictionary *userInfo = [notice userInfo];
    CGRect endFrame = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat _keyboardHeight = (endFrame.origin.y != kCCScreenHeight()) ? endFrame.size.height:0;
    if (!_keyboardHeight) return;
    
    CGRect beginRect = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGRect endRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    if(!(beginRect.size.height > 0 && ( fabs(beginRect.origin.y - endRect.origin.y) > 0))) return;
    //动画执行时长
    _animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//运行轨迹属性
    _animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    //这样就可以与键盘动画无缝衔接了
    [UIView animateWithDuration:_animationDuration delay:0 options:(_animationCurve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        // 修改frame
    } completion:nil];
}

- (void)keyboardWillHide:(NSNotification *)noti {
    //获取键盘的高度
    NSDictionary *userInfo = [noti userInfo];
//    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];    
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    [UIView animateWithDuration:duration delay:0 options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:^{
        // 修改frame
    } completion:nil];
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容