为什么我们要在 UIWebView 中模拟触摸?
我觉得这段代码很有趣。但我不明白为什么我们要在UIWebView中模拟触摸?
enter code here
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
[self performSelector:@selector(touchInView:) withObject:videoView afterDelay:3];
}
- (void)touchInView:(UIView*)view{
UITouch *touch = [[UITouch alloc] initInView:view];
UIEvent *eventDown = [[UIEvent alloc] initWithTouch:touch];
[touch.view touchesBegan:[eventDown allTouches] withEvent:eventDown];
[touch setPhase:UITouchPhaseEnded];
UIEvent *eventUp = [[UIEvent alloc] initWithTouch:touch];
[touch.view touchesEnded:[eventUp allTouches] withEvent:eventUp];
[eventDown release];
[eventUp release];
[touch release];
}
I find this code interesting. But I didn't understood why do we simulate touches in UIWebView?
enter code here
- (void)embedYouTube:(NSString *)urlString frame:(CGRect)frame {
NSString *embedHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
[self performSelector:@selector(touchInView:) withObject:videoView afterDelay:3];
}
- (void)touchInView:(UIView*)view{
UITouch *touch = [[UITouch alloc] initInView:view];
UIEvent *eventDown = [[UIEvent alloc] initWithTouch:touch];
[touch.view touchesBegan:[eventDown allTouches] withEvent:eventDown];
[touch setPhase:UITouchPhaseEnded];
UIEvent *eventUp = [[UIEvent alloc] initWithTouch:touch];
[touch.view touchesEnded:[eventUp allTouches] withEvent:eventUp];
[eventDown release];
[eventUp release];
[touch release];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像我在回答这个问题的评论中发布的那样:它用于“自动启动”YouTube 视频。
like I posted in the comment to answer that question: It is for "autostarting" a youtube-video.