在 UIWebView 中捕获触摸
在 UIWebView 中捕获触摸事件时遇到问题。希望能够感知滚动 HTML 页面上单词的触摸,以便在触摸时将超链接插入 HTML [我完全控制]。显然,一些现有代理已经在感应触摸,因为我收到带有放大镜的“复制或定义”弹出窗口。
我已经能够在没有 UIWebView 的应用程序中的不同自定义 UIViewController 中捕获触摸事件,将:放置
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
在 viewDidLoad 中并放置
- (BOOL) canBecomeFirstResponder
{
NSLog(@"canBecomeFirstResponder");
return YES;
}
在实现中。然后,每当我触摸视图控制器上的任何位置时,都会调用“touchesBegan”。
但相同的代码在自定义视图控制器上无效,其中大部分屏幕都被 UIWebView 覆盖。奇怪的是,底部的小 UIToolBar 中的触摸确实调用了“touches Began”,但 UIWebView 中的某些内容抢占了我的“touchesBegan”。
有没有一种方法可以捕获这些事件而不禁用 UIWebView 想要执行的所有操作?我不想失去滚动和其他功能。
谢谢。新年快乐。
Having trouble capturing touch events in UIWebView. Want to be able to sense touches on words on scrolling HTML page for the purpose of inserting hyperlinks into the HTML [which I fully control] at the point of touch. Obviously some existing agent is already sensing touches, because I get a "Copy or Define" popup with magnifying glass.
I've been able to capture touch events in a different custom UIViewController in my app that has no UIWebView, placing:
self.view.userInteractionEnabled = YES;
self.view.multipleTouchEnabled = YES;
in viewDidLoad and placing
- (BOOL) canBecomeFirstResponder
{
NSLog(@"canBecomeFirstResponder");
return YES;
}
in the implementation. Then "touchesBegan" is called whenever I touch anywhere on the view controller.
But the same code isn't effective on the custom view controller where most of the screen is covered by a UIWebView. Curiously, touches in the small UIToolBar at the bottom does indeed call "touches Began" but something in the UIWebView is pre-empting my "touchesBegan".
Is there a way to capture these events without disabling everything UIWebView wants to do? I wouldn't want to lose scrolling and other features.
Thanks. And Happy New Year.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
捕获
UIWebView
上的触摸是一个痛苦的潘多拉魔盒。由于您只想点击,可能最简单的方法是将UIView
与[UIColor clearColor]
作为背景颜色放置在 Web 视图的顶部。在此视图中,覆盖hitTest:
(而不是覆盖touchesBegan:
等)并使用它来确定点击的位置。hitTest:
返回一个UIView
,因此如果您希望 Web 视图响应点击,只需从hitTest:
覆盖返回 Web 视图即可;否则返回覆盖视图,该视图将接收所有touches...
方法。Capturing touches on a
UIWebView
is a Pandora's Box of pain. Since you want only taps, probably the easiest thing to do is to place aUIView
with[UIColor clearColor]
as the background color over top of the web view. In this view, overridehitTest:
(instead of overridingtouchesBegan:
etc.) and use it to determine the location of the tap.hitTest:
returns aUIView
, so if you want the web view to respond to the tap, just return the web view from yourhitTest:
override; otherwise return the overlay view, which will then receive all thetouches...
methods.