iOS全局处理键盘事件 发表于 2013-01-22 | 分类于 ios | 注册监听键盘事件的通知12345678910111213141516171819[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardDidShowNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardDidHideNotification object:nil]; 在键盘将要出现和隐藏的回调中,加入动画12345678910111213141516171819202122232425262728293031323334353637383940414243444546- (void)keyboardWillShow:(NSNotification *)notif { if (self.hidden == YES) { return; } CGRect rect = [[notif.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat y = rect.origin.y; [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; NSArray *subviews = [self subviews]; for (UIView *sub in subviews) { CGFloat maxY = CGRectGetMaxY(sub.frame); if (maxY > y - 2) { sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, sub.center.y - maxY + y - 2); } } [UIView commitAnimations];}- (void)keyboardShow:(NSNotification *)notif { if (self.hidden == YES) { return; }}- (void)keyboardWillHide:(NSNotification *)notif { if (self.hidden == YES) { return; } [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:0.25]; NSArray *subviews = [self subviews]; for (UIView *sub in subviews) { if (sub.center.y < CGRectGetHeight(self.frame)/2.0) { sub.center = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0); } } [UIView commitAnimations];}- (void)keyboardHide:(NSNotification *)notif { if (self.hidden == YES) { return; }} 本文作者: Xinus 本文链接: http://xinus.wang/2013/01/ios-keyboard-listening/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 许可协议。转载请注明出处!