How to pass the touch event to superview when userInteractionEnabled = YES?

后端 未结 6 1121
青春惊慌失措
青春惊慌失措 2020-12-17 14:24

I have the following setup.

+- XXXCustomControl : UIControl -------+
| A                                    |
|   +- ContentView -------------------+|
|   |         


        
6条回答
  •  一个人的身影
    2020-12-17 15:06

    If you want to handle the event from superview(XXXCustomControl in your case) instead of inner view(ContentView in your case), you can write the following swift code in your superview:

    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        guard let view = super.hitTest(point, with: event) else { return nil }
        if view.isDescendant(of: self) {
            // your custom logic
        }
        return view
    }
    

提交回复
热议问题