Passthrough接触2个UIScrollViews

时间:2014-05-12 10:16:42

标签: ios objective-c uitableview uicollectionview touches

我有2个UIScrollView

的后代

我有UITableView显示数据 我在UICollectionView

之上添加了UITableView
view  
 | - UITableView
 | - UICollectionView

UITableView只能垂直滚动,而UICollectionView只能水平滚动。我只能滚动collectionview不重叠的桌面视图(这是偏离预期的行为),但我需要这样做,以便即使我垂直滑动tableview也可以滚动collectionview {1}}。

我不能简单地将collectionview添加为tableview的子视图,因为其他原因(我知道,这会使这项工作)

还有其他可能让collectionview passthrough与tableview接触吗?

3 个答案:

答案 0 :(得分:4)

您可以尝试创建UICollectionView的子类,并将此代码添加到CustomCollectionView的.m文件中。

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
    UIView *hitView = [super hitTest:point withEvent:event];
    if (hitView == self) {
        return nil;
    } else {
        return hitView;
    }
}

答案 1 :(得分:3)

据我了解,您希望UITableView以及UICollectionView拦截触摸?

我认为您可以尝试将触摸事件从UICollectionView重新发送到UITableView。 (手动调用touchesBegin,touchesMoved,touchesEnded等)

也许覆盖touchesBegan,touchesMoved,touchesEnded方法将适用于您的情况。

您可以尝试使用您的子类覆盖UICollectionView(将属性设置为您的UITableView实例)并使用以下内容实现触摸处理方法:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    if (CGRectContainsPoint(self.tableView.frame, [touch locationInView:self.tableView.superview]) {
       [self.tableView touchesBegan:touches withEvent:event];
    }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesMoved:touches withEvent:event];

        [self.tableView touchesMoved:touches withEvent:event];
    }

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesEnded:touches withEvent:event];

        [self.tableView touchesEnded:touches withEvent:event];
    }

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
        [super touchesCancelled:touches withEvent:event];

        [self.tableView touchesCancelled:touches withEvent:event];
    }

希望它会有所帮助,但我不能100%肯定它。

我也发现了这篇文章,也许它会很有用

http://atastypixel.com/blog/a-trick-for-capturing-all-touch-input-for-the-duration-of-a-touch/

答案 2 :(得分:1)

您可以在collectionview上添加方向垂直的平移手势识别器。在垂直平移事件上,您可以更改表视图的内容偏移以滚动它。