淺談下個(gè)人關(guān)于朋友圈點(diǎn)擊評(píng)論 或者點(diǎn)擊回復(fù)行鍵盤(pán)定位問(wèn)題、鍵盤(pán)切換以及第三方鍵盤(pán)接入的問(wèn)題;
一:首先是鍵盤(pán)定位問(wèn)題:
系統(tǒng)關(guān)于keyboard彈出監(jiān)聽(tīng)有以下幾個(gè)通知(根據(jù)字面意思可以理解一下):
?UIKeyboardWillShowNotification
?UIKeyboardDidShowNotification
UIKeyboardWillHideNotification
UIKeyboardDidHideNotification
UIKeyboardWillChangeFrameNotification //鍵盤(pán)Frame改變的通知
UIKeyboardDidChangeFrameNotification
UIKeyboardWillShowNotification,UIKeyboardWillHideNotification這個(gè)兩個(gè)通知一般搭配使用,但是在鍵盤(pán)切換或者第三方鍵盤(pán)接入的時(shí)候效果不是很好,所以推崇使用UIKeyboardWillChangeFrameNotification這個(gè)通知,可以檢測(cè)鍵盤(pán)彈出,收回,鍵盤(pán)切換或者第三方鍵盤(pán)如搜狗接入后鍵盤(pán)的實(shí)時(shí)變化
二:需要定位的視圖是否是當(dāng)前子視圖
?????? (1)如果是當(dāng)前子視圖只需要在鍵盤(pán)做出改變的時(shí)候監(jiān)聽(tīng)即可:
主要是判斷鍵盤(pán)的坐標(biāo) 在所需要定位視圖的上方還是下方。
??? (2)如果當(dāng)前需要定位的視圖(按鈕或者評(píng)論行)不是當(dāng)前視圖的直接子視圖,比如這樣:
如果針對(duì)cell的label所在行進(jìn)行定位,所以需要把cell中的評(píng)論按鈕或者評(píng)論行轉(zhuǎn)換到當(dāng)前視圖或者window中:self.history_Y_offset = [cell.contentLabel convertRect:cell.contentLabel.bounds toView:window].origin.y;
轉(zhuǎn)換坐標(biāo)可以借鑒這篇文章:坐標(biāo)轉(zhuǎn)換
三:鍵盤(pán)切換或者第三方鍵盤(pán)接入問(wèn)題處理:
if (self.tableview.isDragging) {
return;
}
NSDictionary *userInfo = notification.userInfo;
// 動(dòng)畫(huà)的持續(xù)時(shí)間
double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 鍵盤(pán)的frame
CGRect keyboardRect = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardHeight = keyboardRect.size.height;
CGFloat delta = 0.0;
delta = self.history_Y_offset - ([UIApplication sharedApplication].keyWindow.bounds.size.height - keyboardHeight-40-1);//
CGPoint offset = self.tableview.contentOffset;
offset.y += delta;
if (offset.y < 0) {
offset.y = 0;
}
[self.tableview setContentOffset:offset animated:YES];
MyCell *cell =[self.tableview cellForRowAtIndexPath:self.selectedIndexPath];
UIWindow *window =[UIApplication sharedApplication].keyWindow;
self.history_Y_offset = [cell.contentLabel convertRect:cell.contentLabel.bounds toView:window].origin.y;//在設(shè)置完contentoffset后必須重新設(shè)置所需定位視圖的contentoffset,因?yàn)樵阪I盤(pán)切換過(guò)程中,該視圖的contentoffset會(huì)發(fā)生變化,切換到搜狗鍵盤(pán)也會(huì)發(fā)生變化;
demo 地址:評(píng)論或者回復(fù) 鍵盤(pán)定位問(wèn)題?