近期專案中有一個類似微博的評論轉發功能:螢幕底端放一個 textView 作為輸入框,當它成為第一響應者時,Y 座標需要隨鍵盤高度變化而調整,確保輸入框緊貼鍵盤又不被遮住。
// 鍵盤 frame 改變(位置與尺寸) UIKeyboardWillChangeFrameNotification UIKeyboardDidChangeFrameNotification // 鍵盤即將顯示 / 已顯示 UIKeyboardWillShowNotification UIKeyboardDidShowNotification // 鍵盤即將隱藏 / 已隱藏 UIKeyboardWillHideNotification UIKeyboardDidHideNotification
實作時我只用了 frame 改變的通知,因為它同時涵蓋顯示與隱藏兩種情況:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
通知的 userInfo 中帶著三項關鍵資訊:
notification.userInfo = @{
// 鍵盤彈出或隱藏後的最終 frame
UIKeyboardFrameEndUserInfoKey = NSRect: {{0, 352}, {320, 216}},
// 動畫持續時間
UIKeyboardAnimationDurationUserInfoKey = 0.25,
// 動畫節奏曲線(先快後慢、線性等)
UIKeyboardAnimationCurveUserInfoKey = 7
}
拿到這兩個值後,就能讓自己的位移動畫與鍵盤動畫完全同步:
#pragma mark - 監聽方法
/**
* 鍵盤的 frame 改變時呼叫(顯示、隱藏等)
*/
- (void)keyboardWillChangeFrame:(NSNotification *)notification {
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) {
// 鍵盤已收起到螢幕外,工具列回到最底部
self.toolbar.y = self.view.height - self.toolbar.height;
} else {
self.toolbar.y = keyboardF.origin.y - self.toolbar.height;
}
}];
}
這裡的 self.toolbar 就是那個輸入框容器。
為了讓座標運算更乾淨,我為 UIView 寫了一個分類,把 frame 的常用值包成屬性。
.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
需要的朋友可以直接取用。實務上還有兩個細節值得注意:一是要在 dealloc 或 viewDidDisappear 中移除通知觀察者,否則容易出現訊息發送給已釋放物件的崩潰;二是 iOS 8 之後可以改用 keyboardFrameEndUserInfoKey 配合 convertRect: 取得鍵盤在當前視圖座標系中的位置,這樣在橫向或 iPad 分屏情境下更準確。