//通過設(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];
}
iOS 鍵盤監(jiān)聽與視圖滾動(dòng)
?著作權(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ù)。
【社區(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)容
- 1.先添加對(duì)鍵盤的監(jiān)聽 當(dāng)系統(tǒng)消息出現(xiàn)UIKeyboardWillShowNotification和UIKeybo...
- 1.先添加對(duì)鍵盤的監(jiān)聽 當(dāng)系統(tǒng)消息出現(xiàn)UIKeyboardWillShowNotification和UIKeybo...
- 最近項(xiàng)目中有一個(gè)聊天的頁面,不是本人做的,其中鍵盤視圖有一個(gè)自己的表情視圖,這個(gè)選擇表情的視圖是一個(gè)滾動(dòng)視圖,如果...
- 在學(xué)習(xí)iOS開發(fā)的過程中總是遇見鍵盤出現(xiàn)時(shí),遮蓋了輸出口UITextField,無法看到用戶自己輸出的內(nèi)容。這時(shí)就...
- 由于鍵盤彈出的時(shí)候是用到了通知,所以我們先來看看通知的用法: 先創(chuàng)建兩個(gè)類:LHLPerson和LHLZhaoPi...