MartinMoizard MartinMoizard - 5 months ago 26
iOS Question

UIScrollView and touchesBegan / touchesEnded etc

I have a

UIView
with 2 subviews, let's call it view
A
:


  • a basic
    UIView
    , called
    B

  • a
    UIScrollView
    , called
    C



I overrided the following method in view
A
:


  • - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

  • - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event



When I touch view
B
,
touchesBegan:withEvent:
and
touchesEnded:withEvent:
get called, but if I touch the
UIScrollView
(view
C
), there are not called.

I know this is the expected behaviour, but I am trying to understand why. I could not find any clear answer on that. Any idea?

Answer

You have at least 2 options:

1) "By setting userInteractionEnabled to NO for your scroll view". More info here http://stackoverflow.com/a/25108078/1702413

2) subclass your UIScrollView - for example with this https://dl.dropboxusercontent.com/u/19438780/ClassScrollViewDelegate.zip

UPDATE

Why? Please, read again the documentation:

"Because a scroll .. it must know whether a touch signals an intent to scroll ... it temporarily intercepts a touch-down event ... .. the scroll view cancels any tracking in the subview and performs the scrolling itself"

And most important: **

Subclasses can override the touchesShouldBegin:withEvent:inContentView:, pagingEnabled, and touchesShouldCancelInContentView: methods

From here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIScrollView_Class/index.html#//apple_ref/doc/uid/TP40006922