首先判断点击评论按钮事件 和 回复评论事件(同时注册监听键盘出现和隐藏事件)
然后 获取到 所点击cell的坐标
然后 在键盘出来的时候计算所需要调整的高度
//注册键盘出现NSNotification
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotificationobject:nil];
//注册键盘隐藏NSNotification
[[NSNotificationCenterdefaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotificationobject:nil];
//cell的坐标
@property (nonatomic,assign)CGRect rectSelected;
NSIndexPath * indexPath2 = [NSIndexPathindexPathForItem:index inSection:0];
CGRect rectInTableView = [self.tableViewrectForRowAtIndexPath:indexPath2];
self.rectSelected = [self.tableViewconvertRect:rectInTableViewtoView:[self.tableViewsuperview]];//转化为在self.view 上的坐标
#pragma mark
#pragma mark keyboardWillShow
- (void)keyboardWillShow:(NSNotification *)notification
{
if (_KeySwitch) {
return;
}else{
_KeySwitch =YES;
}
//[0](null) @"UIKeyboardFrameBeginUserInfoKey" : NSRect: {{0, 736}, {414, 293.66666666666674}}
// [1](null) @"UIKeyboardCenterEndUserInfoKey" : NSPoint: {207, 589.16666666666663}
// [2](null) @"UIKeyboardBoundsUserInfoKey" : NSRect: {{0, 0}, {414, 293.66666666666674}}
// [3](null) @"UIKeyboardFrameEndUserInfoKey" : NSRect: {{0, 442.33333333333326}, {414, 293.66666666666674}}
// [5](null) @"UIKeyboardCenterBeginUserInfoKey" : NSPoint: {207, 882.83333333333337}
// [4](null) @"UIKeyboardAnimationDurationUserInfoKey" : (double)0.25
// [6](null) @"UIKeyboardAnimationCurveUserInfoKey" : (long)7
// [7](null) @"UIKeyboardIsLocalUserInfoKey" : (int)1
NSDictionary *userInfo = [notificationuserInfo];
NSValue* aValue = [userInfoobjectForKey:UIKeyboardFrameEndUserInfoKey];//aValueNSConcreteValue * NSRect: {{0, 442.33333333333326}, {414, 293.66666666666674}}0x0000000136c0c4b0
__block CGFloat keyboardHeight = [aValueCGRectValue].size.height;//keyboardHeightCGFloat 293.66666666666674
CGRect keyboardRect = [aValueCGRectValue];//keyboardRectCGRect (origin = (x = 0, y = 0), size = (width = 8, height = 0)) keyboardRect CGRect(origin = (x = 0, y = 442.33333333333326), size = (width = 414, height = 293.66666666666674))
keyboardRect = [self.viewconvertRect:keyboardRectfromView:nil];将rect从view中转换到当前视图中,返回在当前视图中的rect
CGFloat keyboardTop = keyboardRect.origin.y;//keyboardTopCGFloat 442.33333333333326
CGRect newTextViewFrame =self.view.bounds;//newTextViewFrameCGRect (origin = (x = 0, y = 0), size = (width = 414, height = 442.33333333333326)) 键盘上面文字区域的坐标
newTextViewFrame.size.height = keyboardTop -self.view.bounds.origin.y;//上面文字的坐标 高度
NSValue *animationDurationValue = [userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];//animationDurationNSTimeInterval 4.9406564584124654E-324
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
//history_Y_offset btn转换成在self.view上的坐标
CGFloat delta =self.rectSelected.origin.y + self.rectSelected.size.height - ([UIApplication sharedApplication].keyWindow.bounds.size.height - keyboardHeight-100);//deltaCGFloat -54.333333333333258
-------------- 计算tableview要偏移的距离 ----------
CGPoint offset =self.tableView.contentOffset;//offsetCGPoint (x = 3.4540270524485753E-314, y = 2.1232730692355269E-314) 表视图的偏移量
offset.y += delta;//offsetCGPoint (x = 0, y = -118.33333333333326)
if (offset.y <0) {
offset.y = -64;//offsetCGPoint (x = 0, y = -64)
}
[self.tableViewsetContentOffset:offset animated:YES];
}
#pragma mark
#pragma mark keyboardWillHide
- (void)keyboardWillHide:(NSNotification *)notification {
NSValue *animationDurationValue = [notification.userInfoobjectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSTimeInterval animationDuration;
[animationDurationValue getValue:&animationDuration];
[UIViewanimateWithDuration:animationDurationanimations:^{
// [self.chatKeyBoard keyboardDownForComment];
// self.chatKeyBoard.placeHolder = nil;
}];
}