- (void)addKeyBoardNoti {
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardChange:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)removeKeyBoardNoti {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
-(void)keyboardChange:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSTimeInterval animationDuration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGRect keyboardEndFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
[UIView animateWithDuration:animationDuration animations:^{
// 显示
if (notification.name == UIKeyboardWillShowNotification) {
}else{// 隐藏
}
}];
}
// 通过 设置 textField 或者 textView 来设置 键盘通知 和取消通知
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
[self addKeyBoardNoti];
return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView{
[self removeKeyBoardNoti];
}