【发布时间】:2019-08-27 06:42:12
【问题描述】:
如下图,我有一个collectionView,上面有一个maskView。 以及 maskView 上方的右侧视图。
如何实现遮罩视图事件穿透?
我的 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 事件缓存。
【问题讨论】:
标签: ios swift uievent responder-chain