今是昨非

今是昨非

日出江花红胜火,春来江水绿如蓝

iOS開發知識點3——鍵盤

iOS 開發知識點 3—— 鍵盤#

點擊螢幕回收鍵盤是很簡單的,但是在 scrollView 上點擊回收鍵盤,直接調用那個方法就不能實現了

// 我的實現是這樣的
// 首先實現一個繼承自UIScrollView的Category,.m檔案的實現
#import "UIScrollView+UITouch.h"

@implementation UIScrollView (UITouch)

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesBegan:touches withEvent:event];
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesMoved:touches withEvent:event];
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [[self nextResponder] touchesEnded:touches withEvent:event];
    [super touchesEnded:touches withEvent:event];
}

// 然後在要回收鍵盤的界面,導入這個類
#import "UIScrollView+UITouch.h"

// 在touchesBegan方法裡,得到要釋放的textField,調用resignFirstResponder方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    MKInputPhoneNumberCell *inputPhoneNumberCell = (MKInputPhoneNumberCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    [inputPhoneNumberCell.inputPhoneNumberTF resignFirstResponder];
    
    MKPhoneCertifyTableViewCell *phoneCertifyCell = (MKPhoneCertifyTableViewCell *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    [phoneCertifyCell.inputCertifyTF resignFirstResponder];
}
@end

當鍵盤彈出時,有可能會遮蓋住輸入框,之前我採用把 View 放到 scrollView 上來處理,但是後來發現,讓 View 跟著鍵盤動起來效果更好

// 首先註冊通知,彈出鍵盤和鍵盤回收兩個
// 彈出鍵盤時view向上偏移,避免遮蓋輸入框
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];

// 然後實現通知的方法
#pragma mark - keyboard notification -
- (void)keyboardWillShow:(NSNotification *)note {
    NSDictionary *info = [note userInfo];
    // keyboardHeight 為鍵盤高度
    CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    [self animateViewWithKeyboardHeight: keyboardSize.height];
}

- (void)keyboardWillHide:(NSNotification *)note {
    [self animateViewWithKeyboardHeight: 0.0];
}

- (void)animateViewWithKeyboardHeight:(CGFloat)keyboardHeight
{
    NSTimeInterval animationDuration = 0.3f;
    CGFloat width = self.bounds.size.width;
    CGFloat height = self.bounds.size.height;
    // 保持鍵盤和輸入框底部view至少有-10的間距,這裡的間距自己來定
    CGFloat space = -10;
    CGFloat topSize = 0.0;
    CGFloat viewH = _bottomBackView.frame.size.height + _bottomBackView.frame.origin.y + space; // 得到view底部的y
    CGFloat deviceH = self.bounds.size.height;
    // 螢幕高度 - (view底部的y + 鍵盤的高度), 如果>=0,則說明距離足夠,設定view.origin.y = 0;否則則說明view需要上移
    CGFloat animateH = deviceH - viewH - keyboardHeight;
    if (animateH >= 0) {
        topSize = 0;
        CGRect toRect = CGRectMake(0, topSize, width, height);
        self.frame = toRect;
    } else {
        topSize = animateH;
        CGRect toRect = CGRectMake(0, topSize, width, height);
        [UIView animateWithDuration:animationDuration animations:^{
            self.frame = toRect;
        }];
    }
}

// 最後註銷通知
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。