UIPanGestureRecognizer在单元格上被调用两次

时间:2014-08-25 11:45:51

标签: ios objective-c uitableview uiview uipangesturerecognizer

我正在创建一个UITableView,您可以从中拖动到另一个UIView,然后该单元格会转换为自定义UIView。我通过向每个单元格添加UIPanGestureRecognizer来执行此操作,如果是左侧滑动,我会创建自定义UIView并将recognizer提供给它。所有这一切都很好,除了有时会调用多个识别器。它最终拖动自定义视图和它下面的单元格。我不知道两个视图如何同时识别泛。我不能让方法shouldRecognizeSimultaneouslyWithGestureRecognizer返回NO,因为我必须支持表格的scrollView。

以下是拖动代码:

-(void)dragCell:(UIPanGestureRecognizer *)recognizer
{
CGPoint touchLocation = [recognizer locationInView:self.view];

if (recognizer.state == UIGestureRecognizerStateBegan)
{
    gestureWasHandled = NO;
    startPoint = touchLocation;
}
if (recognizer.state == UIGestureRecognizerStateChanged)
{
    float dx = startPoint.x - touchLocation.x;
    float dy = startPoint.y - touchLocation.y;

    BOOL finished = YES;
    BOOL swipeLeft = NO;

    if ((dx > SWIPE_DRAG_MIN) && (ABS(dy) < DRAGLIMIT_MAX))
        swipeLeft = YES;
    else
        finished = NO;

    if (!gestureWasHandled && finished && swipeLeft)
    {
        [symptomsTableView setScrollEnabled:NO];

        UITableViewCell *cell = (UITableViewCell *) recognizer.view;
        NSIndexPath *index = [symptomsTableView indexPathForCell:cell];
        SymptomView *view = [self createSymptomViewAtLocation:touchLocation withSymptom:symptomsInTableView[index.row]];

        [symptomsInTableView removeObjectAtIndex:index.row];
        [cell removeGestureRecognizer:recognizer];
        [view addGestureRecognizer:recognizer];
        gestureWasHandled = YES;
        [symptomsTableView reloadData];
    }
    else if (gestureWasHandled)
    {
        SymptomView *view = (SymptomView *) recognizer.view;
        view.center = touchLocation;
    }
}
if (recognizer.state == UIGestureRecognizerStateEnded)
{
    gestureWasHandled = NO;
    [symptomsTableView setScrollEnabled:YES];

    SymptomView *view = (SymptomView *) recognizer.view;
    [view removeGestureRecognizer:recognizer];
    [symptomsTableView reloadData];
}
}

1 个答案:

答案 0 :(得分:0)

在viewDidLoad方法中添加self.tableview.canCancelContentTouches = YES