无法捕捉触摸在UIWebView的IOS 5

无法捕捉触摸在UIWebView的IOS 5

问题描述:

我是新来的iPhone开发者,无法捕捉触摸在UIWebView的IOS 5

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"); 
} 

任何帮助将不胜感激。

这是一个古老的“问题”。

我这样做的方式是添加一个链接到你的HTML按钮或任何你想要的元素。

然后实现这个UIWebView委托方法(别忘了在你的h文件中添加UIWebViewDelegate)。

-(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; 

} 

BTW 您可以使用NSURL *url = request.URL; 查询的网址,如果你有联系你想成为积极的,或者:当按下在网页视图的链接,所以你可以用它为自己的需求来methos被称为您有多个要执行的操作。

+0

我没有得到你的代码,你能提出我应该在代码中做什么改变吗? – Krunal 2012-07-20 09:46:36

+0

请解释一下“没有得到你的代码”是什么意思? – shannoga 2012-07-20 10:35:54