Handling touches inside UIWebview

前端 未结 10 2076
感情败类
感情败类 2020-11-30 22:46

I have created a subclass of UIWebView , and have implemented the touchesBegan, touchesMoved and touchesEnded methods.

10条回答
  •  自闭症患者
    2020-11-30 23:26

    I'm not sure if this is what you want (it's not what you asked for, but it might work depending on what your end game is), but you could instead interpret the touches in JavaScript from inside the UIWebView, and get javascript to do

    document.location='http://null/'+xCoord+'/'+yCoord; // Null is arbitrary.
    

    Then you can catch that using the UIWebView's delegate method

    - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
    

    And if the request.URL.host (or whatever it is) isEqualToString:@"null" take the relevant action (and return NO instead of YES). You can even add the JS to each page by doing something like:

    - (void)webViewDidFinishLoad:(UIWebView *)webView {
      [webView stringByEvaluatingJavaScriptFromString:@"window.ontouchstart=function(/* ... */);"];
    }
    

    Hope this helps?

提交回复
热议问题