最近頻繁對(duì)鍵盤的遮擋問題進(jìn)行處理,相信所有人都會(huì)頻繁的與鍵盤打交道。于是我在這里把相關(guān)的一些操作進(jìn)行了封裝,多少能減少一些代碼量。

先放上Github地址:https://github.com/superxlx/KeyBoardDemo
從Github上下載以后你可以找到兩個(gè)文件XKeyBoard.h和XKeyBoard.m,這兩個(gè)文件就是我封裝后的文件,將這兩個(gè)文件拖入你的工程。在需要對(duì)鍵盤監(jiān)聽的controller的文件進(jìn)行一下操作:
引入頭文件
#import "XKeyBoard.h"
繼承協(xié)議KeyBoardDlegate,這個(gè)協(xié)議需要實(shí)現(xiàn)兩個(gè)方法:
<pre><code>
/ - (void)keyboardWillShowNotification:(NSNotification *)notification;
/ - (void)keyboardWillHideNotification:(NSNotification *)notification;
</code></pre>
這兩個(gè)方法將分別在鍵盤出現(xiàn)和消失的時(shí)候調(diào)用,但是目前為止你還需要做最后一步:
在viewdidload方法中加入以下代碼
<pre><code>
[XKeyBoard registerKeyBoardShow:self];
[XKeyBoard registerKeyBoardHide:self];
</code></pre>
上面的兩行代碼分別對(duì)鍵盤的出現(xiàn)和消失監(jiān)聽進(jìn)行了注冊(cè)。
好的現(xiàn)在運(yùn)行你的程序吧,在鍵盤出現(xiàn)和消失的時(shí)候是不是調(diào)用了 相關(guān)方法。
XKayBoard還提供了一些其他的有關(guān)鍵盤的屬性:
<pre><code>
/**
- 注冊(cè)鍵盤出現(xiàn)
- @param target 目標(biāo)(self)
*/
- (void)registerKeyBoardShow:(id)target;
/**
- 注冊(cè)鍵盤隱藏
- @param target 目標(biāo)(self)
*/
- (void)registerKeyBoardHide:(id)target;
/**
- @return 返回鍵盤,包括高度、寬度
*/
- (CGRect)returnKeyBoardWindow:(NSNotification )notification;
/*
- @return 返回鍵盤上拉動(dòng)畫持續(xù)時(shí)間
*/
- (double)returnKeyBoardDuration:(NSNotification )notification;
/*
- @return 返回鍵盤上拉,下拉動(dòng)畫曲線
*/
- (UIViewAnimationCurve)returnKeyBoardAnimationCurve:(NSNotification *)notification;
</code></pre>
利用以上屬性就可以在鍵盤出現(xiàn)和消失所調(diào)用的函數(shù)中進(jìn)行相關(guān)操作,比如我是這樣做的:
<pre><code>
-
(void)keyboardWillShowNotification:(NSNotification *)notification
{CGRect keyboardEndFrameWindow = [XKeyBoard returnKeyBoardWindow:notification];
double keyboardTransitionDuration = [XKeyBoard returnKeyBoardDuration:notification];
UIViewAnimationCurve keyboardTransitionAnimationCurve = [XKeyBoard returnKeyBoardAnimationCurve:notification];
[UIView animateWithDuration:keyboardTransitionDuration
delay:0
options:(UIViewAnimationOptions)keyboardTransitionAnimationCurve << 16
animations:^{
CGFloat y =self.view.bounds.size.height - 50;
CGRect frame = CGRectMake(0, y, 320, 50);
frame.origin.y -= keyboardEndFrameWindow.size.height;
self.testview.frame = frame;
} completion:nil];
}
-
(void)keyboardWillHideNotification:(NSNotification *)notification
{
CGRect keyboardEndFrameWindow = [XKeyBoard returnKeyBoardWindow:notification];double keyboardTransitionDuration = [XKeyBoard returnKeyBoardDuration:notification];
UIViewAnimationCurve keyboardTransitionAnimationCurve = [XKeyBoard returnKeyBoardAnimationCurve:notification];
[UIView animateWithDuration:keyboardTransitionDuration
delay:0
options:(UIViewAnimationOptions)keyboardTransitionAnimationCurve << 16
animations:^{
CGPoint cen = self.testview.center;
cen.y += keyboardEndFrameWindow.size.height;
self.testview.center = cen;} completion:nil];
}
</code></pre>
我利用XKeyBoard使鍵盤出現(xiàn)的時(shí)候最下方的UIView上移,消失的時(shí)候這個(gè)UIView有挪回屏幕的最下方,這樣的做法通常在QQ的說說評(píng)論之類的功能等地方會(huì)用到。
這的確是一個(gè)小巧的工具,但也確實(shí)在一些情況下 節(jié)省了我們一些時(shí)間。
如果覺得有用的話,可以在Github上點(diǎn)擊一下Star哦,同時(shí)關(guān)注本人。