How to get touches when parent view has userInteractionEnabled set to NO in iOS

前端 未结 4 600
予麋鹿
予麋鹿 2020-11-28 08:38

When the parent view has userInteractionEnabled=NO, its subviews will not accept touch events even if their userInteractionEnabled property is set to YES.

Is there a

4条回答
  •  孤城傲影
    2020-11-28 09:09

    Swift solution- You need to have a custom view class for the parent view and add the following code to it. Please keep the parent view's user-interactions enabled.

    I hope the following code will help you.

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

提交回复
热议问题