Handle tap event by subview of UIScrollView while scrolling

cocoa-touch, ios, objective-c, uigesturerecognizer, uiscrollview

Solution

You can go with the custom delegates methods as well, using @protocol. Implement those delegate methods in view controller where your UIScrollView has been added.

like in MyContentView:

In touchesBegan method,

[self.delegate contentViewTapped:self];

Now in ContainerView class where scroll view is added, implement that method:

 - (void)contentViewTapped:(MyContentView *)myContentView {

NSLog (@"ContentView no: %d", myContentView.tag); // if tag has been set while adding this view to scrollview.
}

Go through the examples for @protocol.

Hope this is what you required.

Enjoy Coding :)

Problem

I have custom UIScrollView subclass with some content views inside. In some of them I have UITapGestureRecogniser. All works fine when scroll view is not scrolling. But when it scrolling content views does not receive tap action. What is the simplest solution to handle tap action by subview while scroll view is scrolling? Details: `MyScrollView` scrolls horizontally. It contains a lot of content views (e.g. `MyContentView`). Each `MyContentView` has width about one third of `MyScrollView` width. So there are about 3-4 visible `MyContentView` elements at a moment. The main behavior of `MyScrollView` is to 1)make sure that after scrolling one of `MyContentView` elements will be at center of screen and 2)to scroll to center of `MyContentView` if user taps on it. So the main answer I hope to get is how to "properly" implement handling of tap action in `MyContentView` while `MyScrollView` is decelerating. I found some same questions and answers but none of them satisfied me. The best was to implement `gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:` of UITapGestureRecogniser delegate. But in this case I sometimes (when I tap, make smaaaal drag and release finger so tap is steel recognizable(lets called it quasi tap)) have both tap and scroll events and it leads to bugs for me even if scroll view is not scrolling when I begin tap. When user make quasi tap my application tries to scroll to tapped `MyContentView` element and than immediately handle normal scrolling. It seems even more terrible, due to some other functionality start to perform after handling tap (it must not perform when normal scrolling). I need solution where scroll view wait enough to decide it is not tap event and only then make scroll. Otherwise if tap event had recognized scroll must not happen.

Original source