无法在 UIWebView IOS 5 中捕获触摸
Posted
技术标签:
【中文标题】无法在 UIWebView IOS 5 中捕获触摸【英文标题】:Unable to capture touch in UIWebView IOS 5 【发布时间】:2012-07-20 07:04:10 【问题描述】:我是 iPhone 开发者的新手,
TouchCapturingWindow.h
@interface TouchCapturingWindow : UIWindow
NSMutableArray *views;
@private
UIView *touchView;
- (void)addViewForTouchPriority:(UIView*)view;
- (void)removeViewForTouchPriority:(UIView*)view;
@end
TouchCapturingWindow.m
(void)addViewForTouchPriority:(UIView*)view
if ( !views ) views = [[NSMutableArray alloc] init];
[views addObject:view];
- (void)removeViewForTouchPriority:(UIView*)view
if ( !views ) return;
[views removeObject:view];
- (void)sendEvent:(UIEvent *)event
UITouch *touch = [[event allTouches] anyObject];
if (touch.phase == UITouchPhaseBegan)
for ( UIView *view in views )
if ( ![view isHidden] && [view pointInside:[touch locationInView:view] withEvent:event] )
touchView = view;
[touchView touchesBegan:[event allTouches] withEvent:event];
break;
else if (touch.phase == UITouchPhaseMoved)
if ( touchView )
[touchView touchesMoved:[event allTouches] withEvent:event];
else if (touch.phase == UITouchPhaseCancelled)
if ( touchView )
[touchView touchesCancelled:[event allTouches] withEvent:event];
touchView = nil;
else if (touch.phase == UITouchPhaseEnded)
if ( touchView )
[touchView touchesEnded:[event allTouches] withEvent:event];
touchView = nil;
[super sendEvent:event];
@end
DownloadPage.m
- (void)viewDidLoad
[super viewDidLoad];
CGRect webFrame = CGRectMake(0.0, 0.0, 500.0, 500.0);
webView = [[UIWebView alloc] initWithFrame:webFrame];
[webView setBackgroundColor:[UIColor greenColor]];
NSString *urlAddress = @"http://***.com/";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
NSString *t= [webView stringByEvaluatingjavascriptFromString:@"window.getSelection().toString()"];
NSLog(@"selected text=%@",t);
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
NSLog(@"Touches began");
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
NSLog(@"Touches moved");
- (void) touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
NSLog(@"Touches ended");
- (void) touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event
NSLog(@"Touches cancelled");
我们将不胜感激。
【问题讨论】:
【参考方案1】:这是一个古老的“问题”。
我这样做的方法是为您的 html 按钮或您希望的任何元素添加一个链接。
然后实现这个 UIWebView 委托方法(不要忘记在你的 h 文件中添加 UIWebViewDelegate)。当在 webview 中按下链接时会调用此方法,以便您可以根据自己的需要使用它:
-(bool) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
//Do what ever you want here
//prevent the web View from loading the link
return NO;
顺便说一句
您可以使用NSURL *url = request.URL;
来检查 URL,如果您有想要激活的链接,或者您有多个想要实施的操作。
【讨论】:
我没有收到您的代码,您能否建议我应该对我的代码进行哪些更改?以上是关于无法在 UIWebView IOS 5 中捕获触摸的主要内容,如果未能解决你的问题,请参考以下文章
无法在 Xamarin IOS 中捕获 WkWebView 的屏幕
捕捉在 UITableView 中显示的 UIWebView 内触摸