如何启用对其超视图范围之外的视图的触摸?

Adr*_*ian 3 touch clip bounds ios swift

我有一个clipToBounds设置为 false的容器和一个超出其边界的视图。越界视图无法识别触摸事件。

San*_*ula 5

只需将此类添加到您的视图中

class MyView: UIView {

    override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
        for subview in subviews as [UIView] {
            if !subview.isHidden 
               && subview.alpha > 0 
               && subview.isUserInteractionEnabled 
               && subview.point(inside: convert(point, to: subview), with: event) {
                 return true
            }
        }
        return false
    }
}
Run Code Online (Sandbox Code Playgroud)