UITableView:自定义手势使其不再滚动 [英] UITableView: custom gestures make it scrolling no more

查看:69
本文介绍了UITableView:自定义手势使其不再滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UIViewController,其中包含一个UITableView(子类化)和另一个UIView(子类化).它们处于相同的层次结构级别,但是UIView添加到最后,因此它是最前面的.我重写touchesBegan/Moved/Ended以从顶部UIView截取手势:我的目标是获取选定的UITableViewCell,如果两次轻按,则创建要拖动的ImageView.我似乎可以完成它,但是现在即使我转发了触摸事件,我也无法再滚动UITableView了.这是UIView的方法:

I have an UIViewController which contains a UITableView (subclassed) and another UIView (subclassed). They are on the same hierarchy level but the UIView is added last so it is the frontmost. I overrid touchesBegan/Moved/Ended to intercept the Gestures from the top UIView: my goal is to get the selected UITableViewCell and, if double tapped, create an ImageView to be dragged around. I appear to get it done but now I cannot scroll the UITableView anymore, even though I forward the touch events. Here are the methods for the 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中的那些

And here are those in the 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中,我只是:

And in my UITableView I have simply:

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

我肯定会丢失一些东西,但是我不知道什么

I am surely missing something but I do not know what

推荐答案

我找到了解决方法,请在我的自定义UITableView中覆盖触摸手势的方法,以使其在我向其拖动对象时以编程方式滚动.此处是解决方案".

I found a workaround for this, override the methods for touch gestures in my custom UITableView in order to make it scroll programMatically as I drag upon it an object. Here is the 'solution'.

我仍然相信还有另一种更简单的方法可以执行此操作,但是我没有找到它,因此发布并标记为答案"可能会对其他人有所帮助.

I still believe there is another simpler way to do this but I did not find it, so posting this and marking it as an 'answer' might help someone else.

这篇关于UITableView:自定义手势使其不再滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆