iOS開發之監聽鍵盤高度的變化

來源:互聯網
上載者:User

iOS開發之監聽鍵盤高度的變化

最近做的項目中,有一個類似微博中的評論轉寄功能,螢幕底端有一個輸入框用textView來做,當textView成為第一響應者的時候它的Y值隨著鍵盤高度的改變而改變,保證textView緊貼著鍵盤,但又不會被鍵盤擋住。

下面是我實現的方法:(利用通知)

// 鍵盤通知    // 鍵盤的frame發生改變時發出的通知(位置和尺寸)    //    UIKeyboardWillChangeFrameNotification    //    UIKeyboardDidChangeFrameNotification    // 鍵盤顯示時發出的通知    //    UIKeyboardWillShowNotification    //    UIKeyboardDidShowNotification    // 鍵盤隱藏時發出的通知    //    UIKeyboardWillHideNotification    //    UIKeyboardDidHideNotification        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];//在這裡註冊通知

下面是監聽通知:

#pragma mark - 監聽方法/** * 鍵盤的frame發生改變時調用(顯示、隱藏等) */- (void)keyboardWillChangeFrame:(NSNotification *)notification{    //    if (self.picking) return;    /**     notification.userInfo = @{     // 鍵盤彈出\隱藏後的frame     UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 352}, {320, 216}},     // 鍵盤彈出\隱藏所耗費的時間     UIKeyboardAnimationDurationUserInfoKey = 0.25,     // 鍵盤彈出\隱藏動畫的執行節奏(先快後慢,勻速)     UIKeyboardAnimationCurveUserInfoKey = 7     }     */        NSDictionary *userInfo = notification.userInfo;        // 動畫的期間    double duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];        // 鍵盤的frame    CGRect keyboardF = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];        // 執行動畫    [UIView animateWithDuration:duration animations:^{        // 工具條的Y值 == 鍵盤的Y值 - 工具條的高度        if (keyboardF.origin.y > self.view.height) { // 鍵盤的Y值已經遠遠超過了控制器view的高度            self.toolbar.y = self.view.height - self.toolbar.height;//這裡的self.toolbar就是我的輸入框。        } else {            self.toolbar.y = keyboardF.origin.y - self.toolbar.height;        }    }];}


當然,這裡我為UIView寫了一個類別,實現如下:

.h檔案中聲明

@interface UIView (Extension)@property (nonatomic, assign) CGFloat x;@property (nonatomic, assign) CGFloat y;@property (nonatomic, assign) CGFloat width;@property (nonatomic, assign) CGFloat height;@property (nonatomic, assign) CGFloat centerX;@property (nonatomic, assign) CGFloat centerY;@property (nonatomic, assign) CGSize size;@property (nonatomic, assign) CGPoint origin;@end
.m檔案中實現(重寫setter 和 getter)

@implementation UIView (Extension)- (void)setX:(CGFloat)x{    CGRect frame = self.frame;    frame.origin.x = x;    self.frame = frame;}- (void)setY:(CGFloat)y{    CGRect frame = self.frame;    frame.origin.y = y;    self.frame = frame;}- (CGFloat)x{    return self.frame.origin.x;}- (CGFloat)y{    return self.frame.origin.y;}- (void)setCenterX:(CGFloat)centerX{    CGPoint center = self.center;    center.x = centerX;    self.center = center;}- (CGFloat)centerX{    return self.center.x;}- (void)setCenterY:(CGFloat)centerY{    CGPoint center = self.center;    center.y = centerY;    self.center = center;}- (CGFloat)centerY{    return self.center.y;}- (void)setWidth:(CGFloat)width{    CGRect frame = self.frame;    frame.size.width = width;    self.frame = frame;}- (void)setHeight:(CGFloat)height{    CGRect frame = self.frame;    frame.size.height = height;    self.frame = frame;}- (CGFloat)height{    return self.frame.size.height;}- (CGFloat)width{    return self.frame.size.width;}- (void)setSize:(CGSize)size{    CGRect frame = self.frame;    frame.size = size;    self.frame = frame;}- (CGSize)size{    return self.frame.size;}- (void)setOrigin:(CGPoint)origin{    CGRect frame = self.frame;    frame.origin = origin;    self.frame = frame;}- (CGPoint)origin{    return self.frame.origin;}@end
有需要的同行可以直接拿來用


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.