iPhone開發(fā)中如何使UIWebView響應(yīng)Touch事件
iPhone開發(fā)中如何使UIWebView響應(yīng)Touch事件是本文要介紹的內(nèi)容,主要是來介紹如何使UIWebView響應(yīng)Touch事件,具體內(nèi)容來看本文詳解。UIWebView響應(yīng)不了Touch事件,很久以前就很難解決。***有一個(gè)叫做UICWebView的東西,雖然用了private api,但是可以用。
不過UICWebView或者任何用UIWebDocumentView獲得事件的方法在iOS4 100%會(huì)出錯(cuò)(因?yàn)閁IWebDocumentView這個(gè)東西沒了)。用下面的方法可以讓UIWebView響應(yīng)Touch事件,而且不會(huì)影響滾動(dòng)拖拽的bouncing。
如果需要touch的個(gè)數(shù)或者坐標(biāo),也很簡單,不過需要自己修改代碼里的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;
- }
小結(jié):iPhone開發(fā)中如何使UIWebView響應(yīng)Touch事件的內(nèi)容介紹完了,希望通過本文的學(xué)習(xí)能對(duì)你有所幫助!