Get touchesBegan on a subview (UIWebView)

蓝咒 提交于 2019-12-12 16:14:36

问题


I've an UIWebView which is added as a subview in a UIview and I would like to detect when this UIWebView is touched but touchesBegan just don't work.

Any idea ?


回答1:


  1. Subclass UIWebView in your code and then implement touchesBegan in your class.
  2. Add the subclassed webview as a subview of your UIView.
  3. Call super in the subclassed UIWebView

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



回答2:


Subclassing and waiting for touchesBegan won't help you, as it won't get called.
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:)];
        }
    }
}


来源:https://stackoverflow.com/questions/9524011/get-touchesbegan-on-a-subview-uiwebview

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