六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 150|回复: 0

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

[复制链接]

升级  60%

6

主题

6

主题

6

主题

童生

Rank: 1

积分
30
 楼主| 发表于 2013-2-7 19:52:39 | 显示全部楼层 |阅读模式
在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;}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表