2016-11-18 32 views
0

我目前正在使用ChatScreen,並且想添加Archive功能。如何使用UILongPressGestureRecognizer獲得Selected TableView Cell的框架

我的TableView現在有兩個單元格。

我將LongPressGesture添加到打開視圖中。

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]; 

這是長按手勢實現

- (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

嘗試下面的代碼獲取所選單元格

viewDidLoad方法添加的手勢語到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)); 



     } 


    } 
} 
+0

是查看在細胞的indexpath.but顯示,當我滾動cell.it保持在同一indexpath.not改變 –

+1

@DivineChild你需要管理的的UIView的框架使用tableview委託方法'scrollViewDidScroll'。 – Mahesh