How to implement touch events in uiwebview?

不羁岁月 提交于 2019-12-04 13:09:55
pchap10k

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.

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

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.

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:)];
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!