Search code examples
iphoneobjective-ccocoa-touchipaduiscrollview

Prevent UIScrollView to scroll unless it was touched near edges


I have the following situation: A UIScrollView contains a UIWebView. I want the content inside the web view to be touchable, but the scroll view should not scroll unless it was touched near the left or right edge. I have subclassed both UIScrollView and UIWebView to try to override methods like hitTest and pointInside but no go so far.

The only partial success I've had was to override pointInside in my UIScrollView and return NO if the point was not near the edges. Then the UIScrollView stops scrolling, but no touches are sent to the UIWebView.


Solution

  • First of all, you should not subclass UIWebView, as the documentation specifically discourages this practice. You could intercept the touch events by subclassing other classes, such as UIWindow or, in your case, UIScrollView.

    Take a look at this article which demonstrates how to detect touch events on UIWebView. I took a similar but slightly different approach: I subclassed a UIView, which contains a UIWebView (and other controls), and override the hitTest:withEvent: method.