将 UICollectionView 触摸事件传递给其父 UITableViewCell [英] Pass UICollectionView touch event to its parent UITableViewCell

查看:18
本文介绍了将 UICollectionView 触摸事件传递给其父 UITableViewCell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是视图结构:

外部视图是一个UITableView.

UITableViewCell里面,有一个UICollectionView.请注意,集合视图单元格之间有一些黑色间距.

Inside the UITableViewCell, there is a UICollectionView. And notice that there is some black spacing between the collection view cells.

当我点击 UICollectionView 中的间距时,我希望将触摸事件传递给 UITableViewCell.

When I tap the spacing in the UICollectionView, I want the touch event to pass to the UITableViewCell.

推荐答案

@tounaobun 的出色回答.经过测试,它按预期工作:

Excellent answer by @tounaobun. Tested and it works as expected:

1) 如果您点击集合视图上不是项目的任何位置,那么下面的表格单元格将被选中.

1) If you tap anywhere on the collection view that is not an item, then the table cell underneath will select just fine.

2) 如果您点击集合视图中的项目,则表格单元格将不会被选中,您可以正常与集合视图交互(也就是滚动、调用 didSelectItem 等)

2) if you tap on the items in the collection view, then the table cell will not select and you can interact with the collection view normally (aka, scrolling, invoking didSelectItem, etc)

我转成swift供参考:

I converted to swift for reference:

斯威夫特 3:

class TableCellCollectionView: UICollectionView { 

 override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    if let hitView = super.hitTest(point, with: event) {
        if hitView is TableCellCollectionView {
            return nil
        } else {
            return hitView
        }
    } else {
        return nil
    }

}

只需将这个类定义添加到您的代码中(我在实用程序文件中有它),然后将集合视图的类从 UICollectionView 更改为 TableCellCollectionView,您应该一切就绪.

Just add this class definition to your code (I have it in a utility file) and then change the collection view's class from UICollectionView to TableCellCollectionView and you should be all set.

这篇关于将 UICollectionView 触摸事件传递给其父 UITableViewCell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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