如何将手势识别器添加到UIWebView子类?

时间:2010-07-21 17:20:06

标签: iphone uiwebview uigesturerecognizer

如果我将一个手势识别器添加到名为UIWebView的子类webView实例,例如:

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

执行长按时,不会调用-webViewGestureRecognized:选择器。

我已覆盖了委托方法-gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:,但仍未调用长按选择器。

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

我可以做些什么来在网络视图上启用我自己的手势识别器吗?

4 个答案:

答案 0 :(得分:5)

据我所知,UIWebView不应该像Apple文档中提到的那样进行子类化:

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

答案 1 :(得分:4)

如果有人回到这里,我想会加上答案。 您尚未分配代理,因此未调用gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

_longPressRecognizer.delegate = self;

这样做,效果很好。

答案 2 :(得分:2)

您应该在UIWebView中使用javascript来检测手势。 (如果需要,您可以将其传回Objective-C。)这些是Apple's docs,用于检测Javascript中的手势和触摸。我也发现这个article很有用,虽然他使用javascript库来处理事件绑定等。

这是一个捏缩放在独立的UIWebView上的工作示例。

请注意,正是body元素正在侦听事件。在一个短页面上,如果你在下面巨大的未渲染空白中进行事件,它似乎不会捕获它。 (如果有人了解更多信息,请发表评论。)

<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>

答案 3 :(得分:0)

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