How to debug who is eating my touches in UIKit?

前端 未结 7 1487
孤街浪徒
孤街浪徒 2021-01-30 17:16

I can\'t figure out why tapping on text fields and buttons in my view is not working. I\'ve checked all the obvious things like whether userInteractionEnabled is set to YES, whe

7条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 17:45

    A basic way to see what is consuming the touch events is to simply add a debug view and override the hitTest(:event:) method. Attach this class to any to the view in the storyboard.

    class DebugView: UIView {
        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
            let view = super.hitTest(point, with: event)
            print("View")
            print(view)
            return view
        }
    }
    

    Attaching this to the root view of the controller will give you access to the view that is consuming the touch event. Note that the hitTest method will be called twice with every touch event.

提交回复
热议问题