前言:雖然說目前也沒多少APP輸入框會被彈出的鍵盤遮住,但是遇到了也挺麻煩的,所以,我也簡單的整理了一下,代碼如下:

1574840914802133.gif
添加監(jiān)聽
#pragma mark - 鍵盤事件
- (void)initKeyboardObserver
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
}
參數(shù)描述
#define MAINSCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#define MAINSCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define FIRSTRESPONDERBOTTOM 20
//是否正在顯示鍵盤
@property (nonatomic, assign) BOOL isKeyboardShow;
//是否第三方
@property (nonatomic, assign) BOOL isThirdPartKeyboard;
//次數(shù)
@property (nonatomic, assign) NSInteger keyboardShowTime;
//鍵盤動(dòng)畫時(shí)間
@property (nonatomic, assign) CGFloat keyboardAnimateDur;
//鍵盤位置
@property (nonatomic, assign) CGRect keyboardFrame;
//改變的高度
@property (nonatomic, assign) CGFloat changeHeight;
@property (nonatomic, assign) CGFloat oldY;
監(jiān)聽處理
//鍵盤即將顯示
- (void)keyboardWillShow:(NSNotification*)notification
{
NSValue* keyboardBoundsValue = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
CGRect keyboardRect = [keyboardBoundsValue CGRectValue];
NSNumber* keyboardAnimationDur = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
float animationDur = [keyboardAnimationDur floatValue];
_keyboardShowTime++;
//第三方輸入法有bug第一次彈出沒有keyboardRect
if (animationDur > 0.0f && keyboardRect.size.height == 0) {
_isThirdPartKeyboard = YES;
}
//第三方輸入法有動(dòng)畫間隔時(shí)沒有高度
if (_isThirdPartKeyboard) {
// 第三次調(diào)用keyboardWillShow的時(shí)候鍵盤完全展開
if (_keyboardShowTime == 3 && keyboardRect.size.height != 0 && keyboardRect.origin.y != 0) {
_keyboardFrame = keyboardRect;
[self changeForKeyBoradFrame:keyboardRect];
}
if (animationDur > 0.0) {
_keyboardAnimateDur = animationDur;
}
} else {
if (animationDur > 0.0) {
_keyboardFrame = keyboardRect;
_keyboardAnimateDur = animationDur;
[self changeForKeyBoradFrame:keyboardRect];
}
}
}
//鍵盤已經(jīng)顯示
- (void)keyboardDidShow:(NSNotification*)notification {
_isKeyboardShow = YES;
}
//鍵盤即將隱藏
- (void)keyboardWillHide:(NSNotification*)notification
{
NSNumber* keyboardAnimationDur = [[notification userInfo] objectForKey:UIKeyboardAnimationDurationUserInfoKey];
float animationDur = [keyboardAnimationDur floatValue];
_isThirdPartKeyboard = NO;
_keyboardShowTime = 0;
if (animationDur > 0.0 && self.changeHeight<0) {
CGRect animationFrame = self.animationView.frame;
animationFrame.origin.y -= self.changeHeight;
[UIView animateWithDuration:_keyboardAnimateDur animations:^{
self.animationView.frame = animationFrame;
[self.animationView layoutIfNeeded];
[self.animationView layoutSubviews];
} completion:^(BOOL finished) {
self.changeHeight = 0;
self.firstResponderInWindowFrame = CGRectZero;
self.keyboardFrame = CGRectZero;
}];
}
}
//鍵盤已經(jīng)隱藏
- (void)keyboardDidHide:(NSNotification*)notification {
_isKeyboardShow = NO;
}
- (void)changeForKeyBoradFrame:(CGRect)keyboardRect {
//判斷鍵盤是不是低于第一響應(yīng)者+預(yù)設(shè)的空隙
//計(jì)算:鍵盤的Y - 第一響應(yīng)者的Y+高+離鍵盤的空隙 =需要移動(dòng)的高度
CGFloat deviation = keyboardRect.origin.y - (self.firstResponderInWindowFrame.origin.y +self.firstResponderInWindowFrame.size.height +FIRSTRESPONDERBOTTOM);
if (deviation <0) {
self.changeHeight = deviation;
CGRect animatieViewFrame = self.animationView.frame;
//防止多次計(jì)算導(dǎo)致位置錯(cuò)誤
if (self.oldY ==0) {
self.oldY = animatieViewFrame.origin.y;
}
animatieViewFrame.origin.y =self.oldY+deviation;
[UIView animateWithDuration:_keyboardAnimateDur animations:^{
self.animationView.frame = animatieViewFrame;
[self.animationView layoutIfNeeded];
[self.animationView layoutSubviews];
}completion:^(BOOL finished) {
}];
}
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
最后,調(diào)用我感覺也算簡單了
需要傳2個(gè)參數(shù) 一個(gè)是animationView,初始化的時(shí)候就可以傳,另一個(gè)是firstResponder點(diǎn)擊的視圖,在點(diǎn)擊的時(shí)候傳,類似下面這樣。
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[HYKeyboardManager sharedManager].firstResponder =textField;
return YES;
}