iOS 鍵盤監(jiān)聽與視圖滾動(dòng)

//通過設(shè)置該監(jiān)聽,用于獲取鍵盤彈出時(shí)的軌跡屬性及鍵盤frame
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
//通過設(shè)置該監(jiān)聽,用于獲取鍵盤關(guān)閉時(shí)的軌跡屬性及鍵盤frame
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:)
#pragma mark - 鍵盤事件監(jiān)聽
- (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;
    //動(dòng)畫執(zhí)行時(shí)長(zhǎng)
    _animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
//運(yùn)行軌跡屬性
    _animationCurve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    //這樣就可以與鍵盤動(dòng)畫無縫銜接了
    [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];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容