Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 567
subchap Avatar asked Jan 11 '11 19:01

subchap


2 Answers

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

like image 100
Nikso Avatar answered Sep 28 '22 01:09

Nikso


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.

like image 30
mblPrgr Avatar answered Sep 28 '22 00:09

mblPrgr