Search code examples
swiftuiviewtouches

UIView catch touch events and pass throught


I want to implement UIView subclass that will catch all touch events (especially touchesMoved) and pass them through. Is it possible?

class PassthroughView: UIView {

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        let view = super.hitTest(point, with: event)
        return view == self ? nil : view
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touchesBegan")
        super.touchesBegan(touches, with: event)
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touchesMoved") //not called
        super.touchesMoved(touches, with: event)
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("touchesEnded")
        super.touchesEnded(touches, with: event)
    }
}

Solution

  • I use this class:

    class TouchlessView: UIView {
    
        override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
            let buttons = findViews(subclassOf: UIButton.self)
            for button in buttons where button.frame.contains(point) {
                return true
            }
            return false
        }
    }