UILongPressGestureRecognizer不会在处理程序上返回Tag

时间:2014-11-25 12:52:44

标签: xcode uitableview view tags uigesturerecognizer

我正在向这个单元格添加一个手势

UILongPressGestureRecognizer *gesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureSection:)];

[gesture view].tag = indexPath.row;
gesture.minimumPressDuration = 0.5;
gesture.allowableMovement = 600;
[cell addGestureRecognizer:gesture];

我试图获取indexPath.row

  • (void)handleGestureSection:(UIGestureRecognizer *)手势{

    if(gesture.state == UIGestureRecognizerStateEnded){     NSLog(@“Long press Ended”);

    } else if(gesture.state == UIGestureRecognizerStateBegan){     NSInteger i = [手势视图] .tag; } }

但总是回归0。 我做错了什么?

1 个答案:

答案 0 :(得分:0)

你得到0,因为来自UIGestureRecognizer的视图是零。

首先,您应该为每个单元格添加UIGestureRecognizer。

UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(gestureHandler:)];
longPressGesture.delegate = cell;
[longPressGesture setMinimumPressDuration:2.0];
[cell addGestureRecognizer:longPressGesture];

然后,处理gestureHandler:

UIGestureRecognizer *recognizer = (UIGestureRecognizer*) sender;
CGPoint p = [recognizer locationInView:self.tableView];    
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:p];