iPhone开发中如何使UIWebView响应Touch事件

移动开发 iOS
iPhone开发中如何使UIWebView响应Touch事件是本文要介绍的内容,主要是来介绍如何使UIWebView响应Touch事件,具体内容来看本文详解。

iPhone开发中如何使UIWebView响应Touch事件是本文要介绍的内容,主要是来介绍如何使UIWebView响应Touch事件,具体内容来看本文详解。UIWebView响应不了Touch事件,很久以前就很难解决。***有一个叫做UICWebView的东西,虽然用了private api,但是可以用。

不过UICWebView或者任何用UIWebDocumentView获得事件的方法在iOS4 100%会出错(因为UIWebDocumentView这个东西没了)。用下面的方法可以让UIWebView响应Touch事件,而且不会影响滚动拖拽的bouncing。

如果需要touch的个数或者坐标,也很简单,不过需要自己修改代码里的JavaScript。

  1. - (void)viewDidLoad {  
  2.     [super viewDidLoad];  
  3.     selfself.webView.delegate=self;  
  4.     NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];  
  5.     NSString *filePath = [resourcePath stringByAppendingPathComponent:@"test.html"];  
  6.     NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath  encoding:NSUTF8StringEncoding error:nil];  
  7.     NSString *newHTMLString=[htmlstring stringByAppendingString:@"<script language=\"javascript\">
  8. document.ontouchstart=function(){          
  9. document.location=\"myweb:touch:start\";  }; 
  10. document.ontouchend=function(){         
  11.  document.location=\"myweb:touch:end\";  
  12. }; 
  13. document.ontouchmove=function(){        
  14.   document.location=\"myweb:touch:move\"; 
  15.  }  
  16. </script>"
  17. ];  
  18.     [self.webView loadHTMLString:newHTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];  
  19.     [htmlstring release];  
  20. }  
  21. - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)
  22. request navigationType:(UIWebViewNavigationType)navigationType {  
  23.     NSString *requestString = [[request URL] absoluteString];  
  24.     NSArray *components = [requestString componentsSeparatedByString:@":"];  
  25.     if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"myweb"]) {  
  26.         if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])  
  27.         {  
  28.             NSLog(@"%@",[components objectAtIndex:2]);  
  29.         }  
  30.         return NO;  
  31.     }  
  32.     return YES;  

小结:iPhone开发中如何使UIWebView响应Touch事件的内容介绍完了,希望通过本文的学习能对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-18 15:56:08

iPhone开发内存

2013-05-21 09:54:39

Web前端

2011-07-07 15:45:45

iPhone SQLite 数据

2011-07-21 17:00:59

iPhone UIWebView Status Cod

2011-08-22 11:28:46

iPhone开发BMP

2011-08-22 10:49:42

Cocos2d 开发CCLayerTouch事件

2011-09-05 12:49:59

Sencha Touc事件

2011-07-26 09:58:24

2013-04-15 15:22:06

2011-08-02 16:28:40

iPhone Web开发 事件

2011-08-02 11:07:42

iOS开发 UIWebView

2011-09-06 15:53:41

Qt平台GUI

2011-08-05 16:10:31

iPhone 网络 NSURLConne

2022-04-14 16:00:51

事件响应流程企业

2011-07-22 13:23:56

IOS UI ScrollView

2013-04-24 11:15:56

Android开发Touch事件传递机制

2011-08-02 17:58:09

iPhone开发 事件

2011-08-17 15:10:21

iPhone开发Web视图

2015-04-14 09:55:40

2021-02-01 16:55:13

iPhoneTouch ID苹果
点赞
收藏

51CTO技术栈公众号