如何使用UILongPressGestureRecognizer获取Selected TableView Cell的框架

时间:2016-11-18 06:39:16

标签: ios objective-c uigesturerecognizer

我目前正在使用ChatScreen,我想添加归档功能。

我的TableView现在有两个单元格。

我将LongPressGesture添加到Open View。

CGRect myRect = [tblView rectForRowAtIndexPath:indexPath];
UILongPressGestureRecognizer  *lpgr  = [[UILongPressGestureRecognizer alloc]
           initWithTarget:self action:@selector(handleLongPressReceiverText:)];
lpgr.minimumPressDuration = 2.0;
lpgr.delegate = self;
myRect = [tblView rectForRowAtIndexPath:indexPath];
[cell.contentView addGestureRecognizer:lpgr];

这是LongPress手势实现

- (void) handleLongPressReceiverText: (UILongPressGestureRecognizer *)recognizer {
  if  (recognizer.state == UIGestureRecognizerStateBegan) {
       CGPoint p = [lpgr locationInView:tblView];
       NSLog(@"%f",p.x);

       _ReceivertextView.frame = CGRectMake(p.x,p.y,_ReceivertextView.frame.size.width, 40);
       [self.view addSubview: self.ReceivertextView];
       [self.view bringSubviewToFront: self.ReceivertextView];
  }
  if (recognizer.state == UIGestureRecognizerStateEnded)
  {
      NSLog(@"longTouch UIGestureRecognizerStateEnded");
  } 
}

我希望获得特定单元格的确切位置,我使用LongPressGesture进行了挖掘。

对此的任何帮助都非常感谢。

1 个答案:

答案 0 :(得分:1)

尝试使用以下代码获取所选单元格的框架

viewDidLoad方法中将GuestView添加到TableView。

UILongPressGestureRecognizer *longPressGesture  = [[UILongPressGestureRecognizer alloc]
                                                   initWithTarget:self action:@selector(handleLongPress:)];
longPressGesture.delegate = self;
longPressGesture.delaysTouchesBegan = YES;

[self.myTableView addGestureRecognizer:longPressGesture];

处理Logpress事件

-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {

        NSLog(@"Gesture Started ");

        UITableView *selectedTableView=(UITableView *)gestureRecognizer.view;
        CGPoint p = [gestureRecognizer locationInView:selectedTableView];

        //Getting Indexpath
        NSIndexPath *indexPath = [selectedTableView indexPathForRowAtPoint:p];

        if (indexPath == nil)
        {
            NSLog(@"couldn't find index path");
        }
        else
        {
            // get the cell at indexPath (the one you long pressed)
            UITableViewCell *cell =
            [selectedTableView cellForRowAtIndexPath:indexPath];

            //Here is your frame of cell
            NSLog(@"Cell Frame : %@",NSStringFromCGRect(cell.frame));



        }


    }
}