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

4

42

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 any way to still get touch events in subviews?

Lardaceous answered 11/1, 2011 at 19:11 Comment(1)
You may consider this solution: https://mcmap.net/q/219843/-is-it-possible-to-dim-a-uiview-but-allow-touch-of-elements-behind-itMortgagee
R
78

To get a view to let touches pass-through but give its subviews handle touches, let userInteractionEnabled on that view to YES and, instead, use this snippet:

-(id)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    id hitView = [super hitTest:point withEvent:event];
    if (hitView == self) return nil;
    else return hitView;
}

Source: http://cocoaheads.tumblr.com/post/2130871776/ignore-touches-to-uiview-subclass-but-not-to-its

Reprove answered 12/11, 2011 at 12:13 Comment(4)
plz describe how to use it. from where this method i need to call ??Stroll
iOS Wolf this is a UIView method developer.apple.com/library/ios/documentation/UIKit/Reference/…:Photokinesis
Name proposal: CantTouchThisView. :)Jane
@iOS.Wolf you need to create a custom class for the parent View for this. I hope the following code will help you.https://mcmap.net/q/219528/-how-to-get-touches-when-parent-view-has-userinteractionenabled-set-to-no-in-iosPalmerpalmerston
P
6

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
        }
    }
}
Palmerpalmerston answered 14/8, 2018 at 6:37 Comment(0)
F
4

Setting the parent view's userInteractionEnabled property to NO also implicitly sets its subviews userInteractionEnabled properties to NO as well. I don't know of a way to get touch events to the subview using the approach that you have described, but perhaps you could create a view that simply overlays the views that you want users to interact with, but is not the parent of those views.

Flutter answered 7/3, 2011 at 13:10 Comment(0)
P
-2

A work around would be to disable the userinteraction of buttons etc to nil rather than disabling the user interaction of the whole parent view

Phalansterian answered 28/2, 2013 at 6:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.