As the following image,I have a collectionView, with a maskView above it. And a right side view above the maskView.
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.