2个UIScrollViews的穿透触摸

9

我有两个UIScrollView的派生类

我有一个UITableView用于显示数据,另外还有一个UICollectionView位于UITableView之上

view  
 | - UITableView
 | - UICollectionView
UITableView 只能垂直滚动,UICollectionView 只能水平滚动。我只能在 collectionview 不重叠的地方滚动我的 tableview(这是预期行为),但我需要让它这样做,即使我在 collectionview 上垂直滑动也能滚动我的 tableview

我不能简单地将 collectionview 添加为 tableview 的子视图,因为还有其他原因(我知道,这样可以实现)。

是否有其他可能让 collectionview 的触摸事件穿过去达到 tableview 的方法?


你好,你在吗? 你需要放置一个包含两个子视图的主视图,第一个子视图是表格视图,第二个子视图是集合视图,这样才能正常工作。 - Arun
如果将滚动视图直接添加为表格视图的子视图,则它将无法正常工作,滚动会出现问题。 - Arun
你尝试实现的内容截图会有所帮助。 - Rivera
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
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;
    }
}

3

据我理解,您希望UITableView和UICollectionView都能拦截触摸事件?

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

也许重写touchesBegan,touchesMoved,touchesEnded方法对您有帮助。

您可以尝试用属性设置为UITableView实例的子类重写UICollectionView,并使用类似以下代码实现触摸处理方法:

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

希望这能有所帮助,但我不能百分之百确定。

我也找到了这篇文章,或许会有用:

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


1

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


1
但是这样你就无法直接获得弹跳效果。 - Andy Jacobs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,