[英]Detecting touches on an HTML5 <video> in a UIWebView on the iPhone
I would like to be able to detect touch/click events on the tag in the UIWebView. 我希望能够在UIWebView中检测标签上的触摸/点击事件。 But it doesn't look like it responds to that event.
但它看起来并不像它对那个事件做出反应。 Any ideas?
有任何想法吗?
videoElement.addEventListener('touchstart', myFunc, false);
Could also be the 'click' event. 也可能是'点击'事件。
There is a way to do this, but it isn't pretty. 有一种方法可以做到这一点,但它并不漂亮。
UIWebView allows you to run arbitrary javascript, eg [webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"]
. UIWebView允许您运行任意javascript,例如
[webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('html')[0].innerHTML"]
。 So, when the user taps on the videoElement, you can have a javascript run which sets the innerHTML of a specific element, which you can grab from this function. 因此,当用户点击videoElement时,您可以运行javascript来设置特定元素的innerHTML,您可以从该函数中获取该元素。
However, how do you know when to run this function on the Objective-C side? 但是,您如何知道何时在Objective-C端运行此功能? Well, there are two options.
嗯,有两种选择。 First (though I wouldn't suggest it since it would be slow), you can set up a polling function which runs every so often to check the value of the
stringByEvaluatingJavascriptFromString:
function. 首先(虽然我不建议它,因为它会很慢),你可以设置一个轮询函数,它经常运行以检查
stringByEvaluatingJavascriptFromString:
function的值。 The second option, which I think is probably the best one, is to actually subclass WebView to watch touches on the WebView. 第二个选项,我认为可能是最好的选项,实际上是将WebView子类化为观察WebView上的触摸。 So, you can override the following function:
因此,您可以覆盖以下函数:
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
NSString *string = [self stringByEvaluatingJavascriptFromString:@"document.getElementById('videoTapped').innerHTML"]
if ([string isEqual:@"Something"]) {
// do something here
}
}
设置您的视频元素以在点击时加载特定的URL(使用onclick
或只是一个普通锚点),然后在webview的delegate
实现-webView:shouldStartLoadWithRequest:navigationType:
callback以监视该URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.