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

发布于 2024-09-10 12:04:22 字数 901 浏览 1 评论 0原文

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

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北城半夏 2024-09-17 12:04:22

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

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

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

故事灯 2024-09-17 12:04:22

只是想如果有人回到这里我会添加答案。
您尚未分配委托,因此不会调用 gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer:

_longPressRecognizer.delegate = self;

这样做,效果很好。

Just thought I would add the answer if anyone comes back to here.
You have not assigned the delegate, therefore gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: is not called.

_longPressRecognizer.delegate = self;

Do this and it works well.

故事和酒 2024-09-17 12:04:22

您应该在 UIWebView 中使用 javascript 来检测手势。 (如果需要,您可以将其传达回 Objective-C。)这些是 Apple 关于在 Javascript 中检测手势和触摸的文档。我还发现了这篇文章 很有帮助,尽管他使用 javascript 库来处理事件绑定等。

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

请注意,主体元素正在侦听该事件。在一个短页面上,如果您在下面巨大的未渲染的空白区域中执行该事件,则似乎无法捕获它。 (如果有人了解更多,请留言。)

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

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>
明月松间行 2024-09-17 12:04:22
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文