为UIWebView添加单击及左右滑动操作
在HTML所在的字符串中合并以下代码
htmlstring = [htmlstring stringByAppendingString:@"<script language=\"javascript\"> \ document.ontouchstart=function(e){ \ e.preventDefault(); \ if(e.touches.length == 1){ \ document.location=\"myweb:touch:start:\" + e.touches[0].pageX + \":\" + e.touches[0].pageY ; \ } \ }; document.ontouchend=function(e){ \ e.preventDefault(); \ if(e.changedTouches.length == 1){ \ document.location=\"myweb:touch:end:\" + e.changedTouches[0].pageX + \":\" + e.changedTouches[0].pageY ; \ } \ }; document.ontouchmove=function(e){ \ e.preventDefault(); \ if(e.touches.length == 1){ \ document.location=\"myweb:touch:move:\" + e.touches[0].pageX + \":\" + e.touches[0].pageY ; \ } \ } </script>"];设置UIWebView委托后
NSInteger startPointX;NSInteger startPointY;- (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"]) {if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"start"]) {startPointX = [(NSString *)[components objectAtIndex:3] intValue];startPointY = [(NSString *)[components objectAtIndex:4] intValue];}else if ([(NSString *)[components objectAtIndex:2] isEqualToString:@"end"]) {NSInteger endPointX;NSInteger endPointY;endPointX = [(NSString *)[components objectAtIndex:3] intValue];endPointY = [(NSString *)[components objectAtIndex:4] intValue];if (fabsf(endPointY - startPointY) <= 50 && fabsf(endPointX - startPointX) >= 12 ) {if (endPointX > startPointX ) {[self swipeRightAction];}else {[self swipeLeftAction];}}if (startPointX == endPointX && startPointY == endPointY) {[self swipeAction];}} NSLog(@"%@,x:%@,y:%@",[components objectAtIndex:2],[components objectAtIndex:3],[components objectAtIndex:4]); } return NO; } return YES;}