[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];/**< 键盘将要出现的通知 */
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillDismiss:) name:UIKeyboardWillHideNotification object:nil];/**< 键盘将要消失的通知 */
#pragma mark - 键盘处理
//背景的单击手势
- (void)action_gesture:(id)sender {
[self.view endEditing:YES];
}
- (void)keyboardWillShow:(NSNotification *)notification {
[self.view addSubview:_keybordView];
CGFloat curkeyBoardHeight = [[[notification userInfo] objectForKey:@"UIKeyboardBoundsUserInfoKey"] CGRectValue].size.height;
CGRect begin = [[[notification userInfo] objectForKey:@"UIKeyboardFrameBeginUserInfoKey"] CGRectValue];
CGRect end = [[[notification userInfo] objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIView *firstResponder = [[keyWindow performSelector:@selector(firstResponder)] superview];
// 第三方键盘回调三次问题,监听仅执行最后一次
if(begin.size.height>0 && (begin.origin.y-end.origin.y>0)){
NSTimeInterval interal = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//计算 视图应该移动的位置,判断是否遮挡
if (CGRectGetMaxY(firstResponder.frame) > (Screen_Height- curkeyBoardHeight)) {
_offset = Screen_Height- curkeyBoardHeight - CGRectGetMaxY(firstResponder.frame) ;
[UIView animateWithDuration:interal animations:^{
//移动视图
}];
}
}
}
- (void)keyboardWillDismiss:(NSNotification *)notification {
[_keybordView removeFromSuperview];
NSTimeInterval interal = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
[UIView animateWithDuration:interal animations:^{
//移动视图
}];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}