将 UICollectionView 的某些部分的触摸传递给底层视图 [英] Pass touches on some part of UICollectionView to underlying views

查看:24
本文介绍了将 UICollectionView 的某些部分的触摸传递给底层视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先看一下附上的图片:

我的层次结构如下:

  • 界面视图
    • UIButton
    • UICollectionView
      • UICollectionViewCell
        • UICollectionViewCell
          • UICollectionViewCell
            • UICollectionViewCell

            UICollectionView 被添加到 UIVew 作为 H:[collectionView(==270)]|V:|-(70)-[collectionView]-(70)-|代码>.

            UICollectionView is added to UIVew as H:[collectionView(==270)]| and V:|-(70)-[collectionView]-(70)-|.

            我有两个要求:

            1) 只有 UICollectionViewCell 的蓝色部分应该触发 collectionView::didSelectItemAtIndexPath: 方法.我已经成功地实现了

            1) only the blue part of the UICollectionViewCell should trigger the collectionView::didSelectItemAtIndexPath: method. Which I have sucesfully implemented with

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

            在自定义类 UICollectionViewCell 上.customView 属性是单元格的蓝色/红色 UIView.这按预期工作.

            on the custom class UIColectionViewCell. The customView property is the blue/red UIView of the cell. This works as expected.

            2) 我希望在 UICollectionViewCell 的绿色部分或 UICollectionVIew 本身(此处为白色,不透明度为 0.5 的背景)中完成的任何手势(点击/长按/平移/...)传递到超级视图.例如下面的绿松石 UIButtion.绿色部分也不应该滚动 CollectionView .. 它只需要对任何手势完全透明.

            2) I want any gestures (Tap/LongTap/Pan/...) that are done in the green part of the UICollectionViewCell or in the UICollectionVIew itself (the background that is here white with 0.5 opacity) to be passed down to the superview. For Example the turquoise UIButtion below. The green part should also not scroll the CollectionView .. it just has to be completly transparent to any gestures.

            我该怎么做?尝试了很多不同的方法,但没有一个奏效.我知道如何使用常规 UIView 执行此操作,但无法使其在 UICollectionView 及其单元格上工作.

            How can I do that? Tried a lot of different approaches but none of them worked. I know how to do it with a regular UIView, but cannot get it to work on a UICollectionView and its cells.

            请记住,将有其他可交互的 UIViews 或 UIButtions 放置在集合视图的绿色部分下.绿色稍后将成为 UIClearColor.

            Keep in mind that there will be other UIViews or UIButtions that will be interactable and placed under the green part of collection view. The green color will later just be UIClearColor.

            建议只使用较小的 UICollectionView 宽度(蓝色部分的宽度)不是一种选择,因为在某些情况下 UICell 的蓝色/红色部分必须拉伸到单元格的全宽.

            Suggestion to just have the UICollectionView smaller width (width of the blue part) is not an option since the Blue/Red part of UICell has to strecth out to full width of the cell in some cases.

            推荐答案

            以下是上述示例的最终解决方案:

            Here is the final solution for above example:

            1) 只需要红色/蓝色部分是可点击的.这基本保持不变.我参考了蓝色/红色 UIView 名称 customView

            1) Require only red/blue part to be tappable. This stays basically the same. I have a reference to the blue/red UIView names customView

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

            2) 应忽略 UICollectionView 内未在蓝色/红色 UIViews 上完成的任何交互.

            2) Any interactions inside the UICollectionView that are not done on blue/red UIViews should be ignored.

            继承 UICollectionView 并添加覆盖此方法:

            Subclassing UICollectionView and adding overwriting this metod:

            - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
            
                NSIndexPath *indexPath = [self indexPathForItemAtPoint:point];
                LCollectionViewCell *cell = (LCollectionViewCell*)[self cellForItemAtIndexPath:indexPath];
            
                if (cell && [self convertPoint:point toView:cell.customView].x >= 0) {
                    return YES;
                }
                return NO;
            }
            

            这将检查 CGPoint 是否在 customView 上完成.有了这个,我们还支持可变宽度:

            This will check if the CGPoint is done on the customView. With this we are also supporting variable widths:

            这篇关于将 UICollectionView 的某些部分的触摸传递给底层视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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