views:

231

answers:

5

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.

Grateful for some ideas.

+1  A: 

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.

William
A: 

You could do as you've done, and override pointInside. Get the point and event, then call the webView touchesEnded event manually, and pass a NSSet touches object you create along with the event.

Then your webview could take action on that.

just_another_coder
A: 

May Be U can Done it by Use UIScrollView Then Add A UIView in it and then add UIWebView in The UIView.

Ashish Mathur
+1  A: 

See scrollviewsuite sample code from apple. (tapdetectingview)

The ideea to receive any touch events on the scrollview is to add a custom uiview over it and get the taps there. As for scrolling, you should be fine with the scrollview delegate functions which allow you to cancel scrolling if (in your example..) the touches do not correspond to the desired locations to start scrolling. as for the webview which is on top.. you can handle it..

PS. This idea of yours with the scrollview and the webview on top and the touching... it's not that user friendly.. maybe you should reconsider.

Cheers.

lupu1001
A: 

Hello,

I'm not in front of my mac, so this has to be done in pseudo-code.

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
{
    NSRect noScroll = NSMakeRect(10,10,300,460); // No-Touch Zone
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [[allTouches allObjects] objectAtIndex:0];
    CGPoint point = [touch locationInView:[self view];

    if (! NSPointInRect( NSPoint aPoint, NSRect aRect ))
    {
         // Point is NOT within the "No-Touch Zone" Proceed with Execution.
    }
}

I stole this partially from here. So check with that to see where this method goes... I think in the ViewController? You shouldn't have to sub-class anything with this method.

Hope it helps!

Stephen Furlani