Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect touch event on UIScrollView AND on UIView's components [which is placed inside UIScrollView]

I have UIViewController on my simple storyboard IPad project which contains UIScrollView which is placed over entire surface (1024 x 768). I have created 3 XIB files which are UIViews which my application loads on start in viewDidLoad and add them into UIScrollView. Each of these 3 XIB files contains only one UIButton.

This is hierarchy:

~ UIViewController (UIViewControllerClass is class for this UIViewController)

~~ UIScrollView (contains 3 identical UIViews)

~~~ UIView (UIViewClass is File's Owner for this XIB file)

~~~~ UIButton

I would like that my UIViewControllerClass becomes aware of both: touch anywhere on UIScrollView component AND if UIScrollView is touched, if UIButton inside of UIView in UIScrollView is touched, to have information that exactly that button is touched.

I made IBAction inside UIViewClass for touch on UIButton inside UIView in UIScrollView and when I set User Interaction Enabled = YES on all elements (UIViewController, UIView and UIScrollView) this method is called as it should.

But at this point my UIViewControllerClass isn't aware that touch occurred inside UIScrollView on that UIButton. I made touch recognizer like this:

UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTouch)];
touch.numberOfTouchesRequired = 1;

and added it to UIScrollView component. In this way I am able to detect touch event on UIScrollView component in UIViewControllerClass, but touch event handler for UIButton in UIView which is inside UIScrollView isn't called anymore.

So, I need to have these two informations in UIViewControllerClass:

  • Touch on UIScrollView component was made
  • Touch on UIButton in UIView which is inside UIScrollView (if this button was touched) was made

I suppose that attaching touch event recognizer to entire UIScrollView component isn't solution, since it disables all touch event handlers I wrote inside UIViewClass.

I think solution is that somehow touches which are made on components in UIView inside UIScrollView should be sent up to UIViewControllerClass, but I didn't found a way to do this.

If anyone can help me, I'd be very grateful. Thanks in advance.


[edit #1: ANSWER by Zheng]

Tap gesture must have cancelsTouchesInView option set to NO!

For my case above, this line solves everything:

touch.cancelsTouchesInView = NO;

Many thanks to Zheng.

like image 721
uerceg Avatar asked Sep 03 '12 07:09

uerceg


2 Answers

I don't know if this works for you or not, but I've given an answer about touch events for views inside scrollview here:

Dismissing the keyboard in a UIScrollView

The idea is to tell the scrollView not to swallow up all tap gestures within the scroll view area.

I'll paste the code here anyways, hopefully it fixes your problem:

UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];

// prevents the scroll view from swallowing up the touch event of child buttons
tapGesture.cancelsTouchesInView = NO;    

[pageScrollView addGestureRecognizer:tapGesture];

[tapGesture release];

...

// method to hide keyboard when user taps on a scrollview
-(void)hideKeyboard
{
    [myTextFieldInScrollView resignFirstResponder];
}
like image 89
Zhang Avatar answered Nov 08 '22 14:11

Zhang


You can subclass your UIScrollView and override the method - hitTest:withEvent: which is called by the system to determine which view will handle the event. Whenever it is called, you can assume that a touch event occurred inside the scroll view, and by calling the super implementation, you can get the view which would normally process the event.

like image 20
Guillaume Avatar answered Nov 08 '22 14:11

Guillaume