首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

替UIWebView添加单击及左右滑动操作

2012-09-10 
为UIWebView添加单击及左右滑动操作在HTML所在的字符串中合并以下代码htmlstring [htmlstring stringByA

为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;}

热点排行