UITableView:自定义手势使其不再滚动

时间:2010-08-05 18:16:42

标签: iphone uitableview scroll touch

我有一个UIViewController,它包含一个UITableView(子类)和另一个UIView(子类)。它们位于相同的层次结构级别,但UIView最后添加,因此它是最前面的。 我覆盖了touchesBegan / Moved / Ended从顶部UIView拦截手势:我的目标是获取所选的UITableViewCell,如果双击,则创建一个ImageView以进行拖动。 我似乎完成了它,但现在我不能再滚动UITableView,即使我转发了触摸事件。 以下是UIView的方法:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"BO");
    UITouch * touch = [touches anyObject];

    CGPoint tPoint = [touch locationInView:self];
    InventoryViewController * invViewCont = self.viewController;
    UITableView * invTab = invViewCont.inventoryTableView;
    [invTab deselectRowAtIndexPath:[invTab indexPathForSelectedRow] 
                          animated:YES];
    NSArray * cells = [invTab visibleCells];
    BOOL found = NO;
    for (UITableViewCell * cell in cells)
    {
        if (CGRectContainsPoint(cell.frame, tPoint)) 
        {
            [cell touchesBegan:touches withEvent:event];
            found = YES;
            break;
        }
    }


    if (!found)
    {
        [invViewCont.inventoryTableView touchesBegan:touches withEvent:event];
    }
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"Mo");
        UITouch * touch = [touches anyObject];
        CGPoint tPoint = [touch locationInView:self];

        copyObj.center = tPoint;
        InventoryViewController * invViewCont = self.viewController;
        UITableView * invTab = invViewCont.inventoryTableView;

        [invTab deselectRowAtIndexPath:[invTab indexPathForSelectedRow] 
                              animated:YES];
        NSArray * cells = [invTab visibleCells];
        BOOL found = NO;
        for (UITableViewCell * cell in cells)
        {
            if (CGRectContainsPoint(cell.frame, tPoint)) 
            {
                [cell touchesMoved:touches withEvent:event];
                found = YES;
                break;
            }
        }

        if (!found)
        {
            [invViewCont.inventoryTableView touchesMoved:touches withEvent:event];
        }

    }

    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

        UITouch * touch = [touches anyObject];
        if ([touch tapCount] == 2) 
        {
            [self desubCopyView];
        }
        CGPoint tPoint = [touch locationInView:self];
        copyObj.center = tPoint;
        InventoryViewController * invViewCont = self.viewController;
        UITableView * invTab = invViewCont.inventoryTableView;
        [invTab deselectRowAtIndexPath:[invTab indexPathForSelectedRow] 
                              animated:YES];
        NSArray * cells = [invTab visibleCells];
        BOOL found = NO;
        for (UITableViewCell * cell in cells)
        {
            if (CGRectContainsPoint(cell.frame, tPoint)) 
            {
                [cell touchesEnded:touches withEvent:event];
                //[cell.imageView touchesEnded:touches withEvent:event];
                found = YES;
                break;
            }
        }
        if (!found)
        {
            [invViewCont.inventoryTableView touchesEnded:touches withEvent:event];
        }
    }

以下是UITableViewCell中的那些

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch * touch = [touches anyObject];
    if ([touch tapCount] == 2) 
    {
        CGPoint tPoint = [touch locationInView:self]; 
        NSLog(@"CellX %lf CY %lf", tPoint.x, tPoint.y);

        UIGraphicsBeginImageContext(self.bounds.size);
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        UIImageView * newView = [[UIImageView alloc] initWithImage:viewImage];
        [dragArea addSubview:newView];
        dragArea.copyObj = newView;
        [newView release];

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.4];
        dragArea.copyObj.transform = CGAffineTransformMakeScale(1.3, 1.3);
        [UIView commitAnimations];
        tPoint = [self convertPoint:tPoint toView:dragArea];
        dragArea.copyObj.center = tPoint;
    }
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"MOV %@", self.imageView.image);
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"ENDED");
    [super touchesEnded:touches withEvent:event];
}

在我的UITableView中,我简单地说:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"BEGTB");
    [super touchesBegan:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"MOVTB");
    [super touchesMoved:touches withEvent:event];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
        NSLog(@"ENDTB");
    [super touchesEnded:touches withEvent:event];
}

我肯定错过了一些东西,但我不知道是什么

2 个答案:

答案 0 :(得分:0)

我强烈建议您查看UITapGestureRecognizer,而不是自己处理touchesX事件。

答案 1 :(得分:0)

我找到了一个解决方法,在我的自定义UITableView中覆盖触摸手势的方法,以便在我拖动对象时使其滚动programMatically。 Here是'解决方案'。

我仍然相信有另一种更简单的方法可以做到这一点,但我没有找到它,所以张贴这个并将其标记为'答案'可能会帮助其他人。