iOS键盘监听的通知
#pragma mark - 键盘通知回调方法
// 监听键盘的通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrameNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
/*
UIKeyboardAnimationCurveUserInfoKey = 7; 键盘执行动画的节奏
UIKeyboardAnimationDurationUserInfoKey = "0.25"; 键盘弹出动画执行时间
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {375, 258}}"; 键盘尺寸
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {187.5, 796}"; 键盘当前的位置
UIKeyboardCenterEndUserInfoKey = "NSPoint: {187.5, 538}"; 键盘改变后中心的位置
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 667}, {375, 258}}"; 键盘当前的frame
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 409}, {375, 258}}"; 键盘改变后的frame
*/
- (void)keyboardWillChangeFrameNotification:(NSNotification *) notification
{
// NSLog(@"%@",notification.userInfo);
// 1.取出键盘当前的y坐标
CGRect beginFrame = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect endFrame = [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 2.中间的差值
CGFloat minus = endFrame.origin.y - beginFrame.origin.y ;
// 3.根据差值改变控制器view的frame
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += minus;
self.view.frame = viewFrame;
}
#pragma mark - tableView代理方法
/**
* 拖拽tableView方法
*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
// 退出键盘
[self.view endEditing:YES];
}
#pragma mark -dealloc方法写到最后
- (void)dealloc
{
// 移除通知
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
//随输入框的高度改变高度 >> 用于ScorllView中添加的View !!!
- (void)keyboardWillHiden:(NSNotification *)notification{
NSDictionary* info = [notification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
self.loginScorllView.contentInset = contentInsets;
self.loginScorllView.scrollIndicatorInsets = contentInsets;
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, self.loginTitle.frame.origin) ) {
CGPoint scrollPoint = CGPointMake(0.0, self.loginTitle.frame.origin.y-kbSize.height);
[ self.loginScorllView setContentOffset:scrollPoint animated:YES];
}
}
// 通用的 >> 只需改 上面ty的值就行!!
-(void) viewDidAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShowHandler:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHideHandler:)
name:UIKeyboardWillHideNotification
object:nil];
}
-(void) viewDidDisappear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}
- (void)keyboardWillShowHandler:(NSNotification *)notification
{
NSDictionary * userInfo = notification.userInfo;
CGRect frameEndUserInfo = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat cure = [userInfo[UIKeyboardAnimationCurveUserInfoKey] floatValue];
// CGRect toFrame = self.frame;
CGFloat ty = frameEndUserInfo.origin.y-frameEndUserInfo.origin.y+70;
if (self.view.y != ty) {
[UIView animateWithDuration:duration animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:cure];
self.view.y = -ty;
// self.frame = toFrame;
}];
}
}
- (void)keyboardWillHideHandler:(NSNotification *)notification
{
NSDictionary * userInfo = notification.userInfo;
CGRect frameEndUserInfo = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];
CGFloat cure = [userInfo[UIKeyboardAnimationCurveUserInfoKey] floatValue];
// CGRect toFrame = self.frame;
CGFloat ty = frameEndUserInfo.origin.y -frameEndUserInfo.origin.y-64;
if (self.view.y != ty) {
[UIView animateWithDuration:duration animations:^{
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:cure];
self.view.y = -ty;
// self.frame = toFrame;
}];
}
}