如何在uiwebview中实现触摸事件?

时间:2020-12-10 00:18:32

I have tried various solutions provided on this site and others to implement touch events on uiwebview. But still I am not able to do this. Actually, i have created a article reader application. Here, I have added a uiwebview on the normal uiview. Now, I want to trace some user touch events on that particular webview. When I do this on normal view, it works perfectly. But if I try it on webview. it stops working. The solutions I tried before are

我尝试了这个站点和其他站点上提供的各种解决方案来实现uiwebview上的触摸事件。但我还是做不到。实际上,我已经创建了一个文章阅读器应用程序。在这里,我在普通的uiview上添加了一个uiwebview。现在,我想在这个特定的webview上跟踪一些用户触摸事件。当我在法线上做这个的时候,它是完美的。但如果我在webview上试试。它停止工作。我以前尝试过的解决方法是

  1. implementing touch methods like touchbegan touchended touchmoved touch cancelled
  2. 实现触摸方法,如touchbegan touchended touchmoved touch取消

2 implementing uigesturerecognizer 3 implementing window touch events like send event

2实现uigesturerecognizer 3实现窗口触摸事件,如发送事件

Now If anyone can help me or tell me where I am doing wrong or a new solution(other than this), then I will be thankful.

现在如果有人能帮我,或者告诉我哪里做错了,或者有新的解决方案(除了这个),我会很感激。

2 个解决方案

#1


2  

Put a transparent UIVIew on top of your UIWebView to capture touches. You can then act on them or optionally pass them down to the UIWebView using the touchesBegan deletage method.

将一个透明的UIVIew放在你的UIWebView的顶部以捕获触摸。然后,您可以对它们进行操作,或者可以选择使用touchesBegan deletage方法将它们传递到UIWebView。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.myUIWebView touchesBegan:touches withEvent:event];
}

Check this previous post for details: iOS - forward all touches through a view

查看这篇文章的详细信息:iOS - forward all touchesthrough a view

#2


0  

I subclassed UIWebView and just "leeched" onto its gesture recognizers in subviews 2 levels deep (you could go recursively but thats enough for iOS6-7). Then you can do whatever you want with the touch location and gesture recognizer's state.

我对UIWebView进行了子类化,然后将它的手势识别器“下载”到子视图2层深处(你可以递归地进行,但iOS6-7已经足够了)。然后你可以用触摸位置和手势识别器的状态做任何你想做的事情。

for (UIView* view in self.subviews) {
    for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
        [recognizer addTarget:self action:@selector(touchEvent:)];
    }
    for (UIView* sview in view.subviews) {
        for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
            [recognizer addTarget:self action:@selector(touchEvent:)];
        }
    }
}

#1


2  

Put a transparent UIVIew on top of your UIWebView to capture touches. You can then act on them or optionally pass them down to the UIWebView using the touchesBegan deletage method.

将一个透明的UIVIew放在你的UIWebView的顶部以捕获触摸。然后,您可以对它们进行操作,或者可以选择使用touchesBegan deletage方法将它们传递到UIWebView。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.myUIWebView touchesBegan:touches withEvent:event];
}

Check this previous post for details: iOS - forward all touches through a view

查看这篇文章的详细信息:iOS - forward all touchesthrough a view

#2


0  

I subclassed UIWebView and just "leeched" onto its gesture recognizers in subviews 2 levels deep (you could go recursively but thats enough for iOS6-7). Then you can do whatever you want with the touch location and gesture recognizer's state.

我对UIWebView进行了子类化,然后将它的手势识别器“下载”到子视图2层深处(你可以递归地进行,但iOS6-7已经足够了)。然后你可以用触摸位置和手势识别器的状态做任何你想做的事情。

for (UIView* view in self.subviews) {
    for (UIGestureRecognizer* recognizer in view.gestureRecognizers) {
        [recognizer addTarget:self action:@selector(touchEvent:)];
    }
    for (UIView* sview in view.subviews) {
        for (UIGestureRecognizer* recognizer in sview.gestureRecognizers) {
            [recognizer addTarget:self action:@selector(touchEvent:)];
        }
    }
}