【问题标题】:iOS: How to implement the mask view event penetrate?iOS:如何实现遮罩视图事件穿透?
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      • 2021-08-28
      • 2011-07-04
      相关资源
      最近更新 更多