iOS 鍵盤擋住視圖優(yōu)化

前言:雖然說目前也沒多少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;
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 小星先秦:佚名嘒彼小星,三五在東。肅肅宵征,夙夜在公。寔命不同。嘒彼小星,維參與昴。肅肅宵征,抱衾與裯。寔命不猶。...
    To者也閱讀 1,194評論 0 1
  • 價(jià)值人數(shù)頻次=品牌要高頻次和小驚喜,不要憋大招,個(gè)人品牌是這樣,產(chǎn)品思維也是這樣,做出最小化產(chǎn)品,快速迭代打造個(gè)人...
    生活建筑師閱讀 163評論 0 2
  • 我害怕比較,一比較就有高低,一比較就會有差距,一比較就會有失落,太多不開心源于比較,為什么想要跟別人比呢,因?yàn)橛?..
    YM0202閱讀 257評論 0 0

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