iOS 键盘自适应评论cell下方

首先判断点击评论按钮事件  和  回复评论事件(同时注册监听键盘出现和隐藏事件)

然后 获取到 所点击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];rectview中转换到当前视图中,返回在当前视图中的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;

    }];

}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值