0

如下图,我有一个collectionView,上面有一个maskView。以及 maskView 上方的右侧视图。

图像

如何实现遮罩视图事件穿透?</p>

我的 PM 的想法是,当 collectionView 上方的掩码视图时,collectionViewCell 是可点击的。(有点奇怪)

掩码视图已经接管了 collectionView 的事件。

我想我应该覆盖事件响应者链。

我试图处理override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?

class Mask: UIView {
// ...
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let views = superview?.subviews else {
            return self
        }
        for view in views{
            if type(of: view) == UICollectionView.self{
                return view
            }
        }
        return self
    }
}

没有按命令工作,collectionView 只是滚动。

如何解决?


我稍微改进了代码。

override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let views = superview?.subviews else {
            return self
        }
        for view in views{
            if type(of: view) == UICollectionView.self{
                let collectionView = view as! UICollectionView
                for cell in collectionView.visibleCells{
                    if cell.frame.contains(point){
                        return cell
                    }
                }
            }
        }
        return self
    }

它不是很锋利。切换单击的单元格时会放弃某些事件。就像 Apple 做了一个 hitTest 事件缓存一样。

4

1 回答 1

1

如果我理解正确并且掩码视图位于 collectionView 之上并且它获取事件,则您可以将掩码视图 userInteractionEnabled 设置为 false。

这样,事件将被发送到下面的层,直到有人处理它。

于 2019-08-27T06:50:15.710 回答