如何使用UILongPressGestureRecognizer获得Selected TableView Cell的框架
问题描述:
我目前正在使用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
窃听的确切位置。
任何帮助,这是高度赞赏。
答
尝试下面的代码获取所选单元格
在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));
}
}
}
是查看在细胞的indexpath.but显示,当我滚动cell.it保持在同一indexpath.not改变 –
@DivineChild你需要管理的的UIView的框架使用tableview委托方法'scrollViewDidScroll'。 – Mahesh