iPhone開發中如何使UIWebView響應Touch事件
iPhone開發中如何使UIWebView響應Touch事件是本文要介紹的內容,主要是來介紹如何使UIWebView響應Touch事件,具體內容來看本文詳解。UIWebView響應不了Touch事件,很久以前就很難解決。***有一個叫做UICWebView的東西,雖然用了private api,但是可以用。
不過UICWebView或者任何用UIWebDocumentView獲得事件的方法在iOS4 100%會出錯(因為UIWebDocumentView這個東西沒了)。用下面的方法可以讓UIWebView響應Touch事件,而且不會影響滾動拖拽的bouncing。
如果需要touch的個數或者坐標,也很簡單,不過需要自己修改代碼里的JavaScript。
- - (void)viewDidLoad {
- [super viewDidLoad];
- selfself.webView.delegate=self;
- NSString *resourcePath = [ [NSBundle mainBundle] resourcePath];
- NSString *filePath = [resourcePath stringByAppendingPathComponent:@"test.html"];
- NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
- NSString *newHTMLString=[htmlstring stringByAppendingString:@"<script language=\"javascript\">
- document.ontouchstart=function(){
- document.location=\"myweb:touch:start\"; };
- document.ontouchend=function(){
- document.location=\"myweb:touch:end\";
- };
- document.ontouchmove=function(){
- document.location=\"myweb:touch:move\";
- }
- </script>"
- ];
- [self.webView loadHTMLString:newHTMLString baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
- [htmlstring release];
- }
- - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)
- request navigationType:(UIWebViewNavigationType)navigationType {
- NSString *requestString = [[request URL] absoluteString];
- NSArray *components = [requestString componentsSeparatedByString:@":"];
- if ([components count] > 1 && [(NSString *)[components objectAtIndex:0] isEqualToString:@"myweb"]) {
- if([(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
- {
- NSLog(@"%@",[components objectAtIndex:2]);
- }
- return NO;
- }
- return YES;
- }
小結:iPhone開發中如何使UIWebView響應Touch事件的內容介紹完了,希望通過本文的學習能對你有所幫助!