0

As the following image,I have a collectionView, with a maskView above it. And a right side view above the maskView.

img

How to implement the mask view event penetrate?

My PM's idea is that the collectionViewCell is clickable when a mask view above the collectionView. ( a little weird )

The mask view has taken over the collectionView's event.

I think I should override the event responder chain.

I tried to handle the 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
    }
}

Not worked as commanded, the collectionView just scrolls.

How to solve it?


I improved the code a little.

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
    }

It is not very sharp. Some events are abandoned when switching the cells being clicked. Just like Apple did a hitTest event cache.

dengST30
  • 1,898
  • 10
  • 19

1 Answers1

1

If I understood correctly and the mask view is on top of the collectionView and it gets the events you can just set the mask view userInteractionEnabled to false.

This way the event will be sent to the layer below until someone handles it.

User123335511231
  • 11,114
  • 4
  • 16
  • 20