views:

346

answers:

2

If I add a gesture recognizer to a subclassed UIWebView instance called webView, e.g.:

UILongPressGestureRecognizer *_longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(webViewGestureRecognized:)];
_longPressRecognizer.allowableMovement = 20;
_longPressRecognizer.minimumPressDuration = 1.0f;
[webView addGestureRecognizer:_longPressRecognizer];
[_longPressRecognizer release], _longPressRecognizer = nil; 

The -webViewGestureRecognized: selector is not called when I perform a long press.

I have overridden the delegate method -gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: but the long-press selector is still not called.

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

Is there anything I can do to enable my own gesture recognizer on the web view?

+1  A: 

as far as I know UIWebView shouldnt be subclassed as mentioned in Apple docs:

http://developer.apple.com/iphone/library/documentation/uikit/reference/UIWebView_Class/Reference/Reference.html

YNK
A: 

You should use javascript in the UIWebView to detect the gestures. (You can then communicate that back to the Objective-C if you need to.) These are Apple's docs on detecting gestures and touches in Javascript. I also found this article helpful, although he uses a javascript library to deal with event binding, etc.

Here's a working example of pinching to zoom on a UIWebView that stands alone.

Note that it is the body element is listening for the event. On a short page it appears not to catch it if you do the event in the vast un-rendered whitespace below. (If anyone knows more about it, please leave a comment.)

<body id='body'>
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 

<script type="text/javascript" charset="utf-8">

    body = document.getElementById('body');

    // (in percents)
    zoom = 100;
    maxZoom = 200;
    minZoom = 50;
    zoomIncrement = 25;

    function gestureEnd(event) {
        var newZoom;
        if (event.scale > 1.0) {
            // increase size
            newZoom = zoom + zoomIncrement;
        } else {
            // decrease size
            newZoom = zoom - zoomIncrement;
        }

        // don't exceed zoom boundaries
        if (newZoom > maxZoom || newZoom < minZoom) {
            return;
        }
        zoom = newZoom;
        body.style.webkitTextSizeAdjust = zoom+"%";
    }

    body.addEventListener("gestureend", gestureEnd, false);

    </script>
</body>
zekel
I don't have control over the web content, so this is not helpful for my use case.
Alex Reynolds