UIWebView的使用总结

本文总结了UIWebView的基本用法,包括设置透明、禁止滚动、获取高度及使用JS添加事件响应。详细介绍了如何通过JavaScript实现触摸事件的捕捉与响应。

UIWebView的使用总结

分类: iPhone Object-c 18558人阅读 评论(2) 收藏 举报

设置UIWebView透明

  1. [webview setBackgroundColor:[UIColor clearColor]];  
  2.   
  3. webview.opaque = NO;  
[webview setBackgroundColor:[UIColor clearColor]];

webview.opaque = NO;


禁止UIWebView滚动 

  1. webView.scrollView.bounces = NO; //__IPHONE_5_0  
webView.scrollView.bounces = NO; //__IPHONE_5_0
  1. UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];  
  2. scrollView.bounces = NO;   
UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];
scrollView.bounces = NO; 



获取UIWebView高度

  1. - (void)webViewDidFinishLoad:(UIWebView *)webView1  
  2. {  
  3.     UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];  
  4.     CGFloat webViewHeight = [scrollView contentSize].height;  
  5.     NSString *curHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];  
  6.   
  7.     CGRect newFrame = webView.frame;  
  8.     newFrame.size.height = webViewHeight;  
  9.     webView.frame = newFrame;   
  10. }  
- (void)webViewDidFinishLoad:(UIWebView *)webView1
{
    UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0];
    CGFloat webViewHeight = [scrollView contentSize].height;
    NSString *curHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"];

    CGRect newFrame = webView.frame;
    newFrame.size.height = webViewHeight;
    webView.frame = newFrame; 
}



使用JS给UIWebView添加事件响应

1.首先定义事件的JavaScript

  1. // timeStamp 微秒   
  2. static NSString * const webTouchJavaScriptString =  
  3.     @"<script language=\"javascript\">document.ontouchstart=function(event){\  
  4.     x=event.targetTouches[0].clientX;\  
  5.     y=event.targetTouches[0].clientY;\  
  6.     time=event.timeStamp;\  
  7.     document.location=\"wiweb:touch:start:\"+x+\":\"+y+\":\"+time;};\  
  8.     document.ontouchmove=function(event){\  
  9.     x=event.targetTouches[0].clientX;\  
  10.     y=event.targetTouches[0].clientY;\  
  11.     document.location=\"wiweb:touch:move:\"+x+\":\"+y;};\  
  12.     document.ontouchcancel=function(event){\  
  13.     document.location=\"wiweb:touch:cancel\";};\  
  14.     document.ontouchend=function(event){\  
  15.     time=event.timeStamp;\  
  16.     document.location=\"wiweb:touch:end:\"+time;}; </script>";  
// timeStamp 微秒
static NSString * const webTouchJavaScriptString =
    @"<script language=\"javascript\">document.ontouchstart=function(event){\
    x=event.targetTouches[0].clientX;\
    y=event.targetTouches[0].clientY;\
    time=event.timeStamp;\
    document.location=\"wiweb:touch:start:\"+x+\":\"+y+\":\"+time;};\
    document.ontouchmove=function(event){\
    x=event.targetTouches[0].clientX;\
    y=event.targetTouches[0].clientY;\
    document.location=\"wiweb:touch:move:\"+x+\":\"+y;};\
    document.ontouchcancel=function(event){\
    document.location=\"wiweb:touch:cancel\";};\
    document.ontouchend=function(event){\
    time=event.timeStamp;\
    document.location=\"wiweb:touch:end:\"+time;}; </script>";


2.组织字符串

  1. NSString *webviewText = @"<style>body{margin:0;background-color:transparent;color:#000000;word-wrap:break-word;word-break:break-all;font:18px/22px system}</style>";  
  2. NSString *htmlString = [webviewText stringByAppendingFormat:@"%@", @"自定SDFSDFSDFSDF义字体fsdgjdlagj asdkgjksdh卡号给卡仕达;逛了会街啊啊流口水 http://www.baidu.com 的感觉卡拉;四大金刚;拉开始打工绿卡;但是结果来看;就爱上的看过就卡的;上来讲赶快来;啊都是经过后ihgoiadsg;肯定是噶上的好;拉克丝的价格爱国阿斯顿改了可"];  
  3.   
  4. NSString *newHTMLString=[htmlString stringByAppendingString:webTouchJavaScriptString];  
    NSString *webviewText = @"<style>body{margin:0;background-color:transparent;color:#000000;word-wrap:break-word;word-break:break-all;font:18px/22px system}</style>";
    NSString *htmlString = [webviewText stringByAppendingFormat:@"%@", @"自定SDFSDFSDFSDF义字体fsdgjdlagj asdkgjksdh卡号给卡仕达;逛了会街啊啊流口水 http://www.baidu.com 的感觉卡拉;四大金刚;拉开始打工绿卡;但是结果来看;就爱上的看过就卡的;上来讲赶快来;啊都是经过后ihgoiadsg;肯定是噶上的好;拉克丝的价格爱国阿斯顿改了可"];
    
    NSString *newHTMLString=[htmlString stringByAppendingString:webTouchJavaScriptString];

3.事件响应

  1. - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType  
  2. {  
  3.     BOOL result = YES;  
  4.     NSURL *requestURL =[[request URL] retain];  
  5.     NSString *requestString = [[request URL] absoluteString];  
  6.     static BOOL bstart = NO;  
  7.     static BOOL bmove = NO;  
  8.     static double lasttime = 0;  
  9.       
  10.     NSString *str = [requestURL scheme];  
  11.     if ( ([str isEqualToString:@"http"] || [str isEqualToString:@"https"] || [str isEqualToString:@"mailto"] || [str isEqualToString:@"tel"])  
  12.         && (navigationType == UIWebViewNavigationTypeLinkClicked) )  
  13.     {  
  14.         result = ![[UIApplication sharedApplication] openURL:[requestURL autorelease]];  
  15.     }  
  16.     else  
  17.     {  
  18.         [requestURL release];  
  19.         NSArray *components = [requestString componentsSeparatedByString:@":"];  
  20.         if ([components count] > 2   
  21.             && [(NSString *)[components objectAtIndex:0] isEqualToString:@"wiweb"]  
  22.             && [(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])  
  23.         {  
  24.             NSString *eventString=[components objectAtIndex:2];  
  25.             if ([eventString isEqualToString:@"start"])  
  26.             {  
  27.                 float pointX=[[components objectAtIndex:3] floatValue];  
  28.                 float pointY=[[components objectAtIndex:4] floatValue];  
  29.                 double time=[[components objectAtIndex:5] doubleValue];  
  30.                 CGPoint aPoint = CGPointMake(pointX, pointY);  
  31.                 NSLog(@"start: %@", NSStringFromCGPoint(aPoint));  
  32.                 NSLog(@"start time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);  
  33.                 lasttime = time;  
  34.                 bstart = YES;  
  35.                 bmove = NO;  
  36.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  37.             }  
  38.             else if ([eventString isEqualToString:@"move"])  
  39.             {  
  40.                 float pointX=[[components objectAtIndex:3] floatValue];  
  41.                 float pointY=[[components objectAtIndex:4] floatValue];  
  42.                 CGPoint aPoint=CGPointMake(pointX, pointY);  
  43.                 NSLog(@"move: %@", NSStringFromCGPoint(aPoint));  
  44.                 bmove = YES;  
  45.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  46.             }  
  47.             else if ([eventString isEqualToString:@"cancel"])  
  48.             {  
  49.                 NSLog(@"cancel");  
  50.                 bstart = NO;  
  51.                 bmove = NO;  
  52.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  53.             }  
  54.             else if ([eventString isEqualToString:@"end"])  
  55.             {  
  56.                 double time=[[components objectAtIndex:3] doubleValue];  
  57.                 NSLog(@"end");  
  58.                 NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);  
  59.                 NSLog(@"end time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);  
  60.                 if (bstart && !bmove)  
  61.                 {  
  62.                     if (time - lasttime > 400)  
  63.                     {  
  64.                         NSLog(@"LongPress!!!!!!");  
  65.                     }  
  66.                     else  
  67.                     {  
  68.                         NSLog(@"Click!!!!!!");  
  69.                     }  
  70.                 }  
  71.                 bstart = NO;  
  72.                 bmove = NO;  
  73.             }  
  74.               
  75.             return NO;  
  76.         }  
  77.     }  
  78.       
  79.     NSURL *url = [request URL];  
  80.     NSString *curUrl= [url absoluteString];  
  81.     NSLog(@"cururl: %@", curUrl);  
  82.       
  83.     return result;  
  84. }  
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    BOOL result = YES;
    NSURL *requestURL =[[request URL] retain];
    NSString *requestString = [[request URL] absoluteString];
    static BOOL bstart = NO;
    static BOOL bmove = NO;
    static double lasttime = 0;
    
    NSString *str = [requestURL scheme];
    if ( ([str isEqualToString:@"http"] || [str isEqualToString:@"https"] || [str isEqualToString:@"mailto"] || [str isEqualToString:@"tel"])
        && (navigationType == UIWebViewNavigationTypeLinkClicked) )
    {
        result = ![[UIApplication sharedApplication] openURL:[requestURL autorelease]];
    }
    else
    {
        [requestURL release];
        NSArray *components = [requestString componentsSeparatedByString:@":"];
        if ([components count] > 2 
            && [(NSString *)[components objectAtIndex:0] isEqualToString:@"wiweb"]
            && [(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"])
        {
            NSString *eventString=[components objectAtIndex:2];
            if ([eventString isEqualToString:@"start"])
            {
                float pointX=[[components objectAtIndex:3] floatValue];
                float pointY=[[components objectAtIndex:4] floatValue];
                double time=[[components objectAtIndex:5] doubleValue];
                CGPoint aPoint = CGPointMake(pointX, pointY);
                NSLog(@"start: %@", NSStringFromCGPoint(aPoint));
                NSLog(@"start time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);
                lasttime = time;
                bstart = YES;
                bmove = NO;
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
            }
            else if ([eventString isEqualToString:@"move"])
            {
                float pointX=[[components objectAtIndex:3] floatValue];
                float pointY=[[components objectAtIndex:4] floatValue];
                CGPoint aPoint=CGPointMake(pointX, pointY);
                NSLog(@"move: %@", NSStringFromCGPoint(aPoint));
                bmove = YES;
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
            }
            else if ([eventString isEqualToString:@"cancel"])
            {
                NSLog(@"cancel");
                bstart = NO;
                bmove = NO;
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
            }
            else if ([eventString isEqualToString:@"end"])
            {
                double time=[[components objectAtIndex:3] doubleValue];
                NSLog(@"end");
                NSLog(@"bstart: %d -- bmove: %d", bstart, bmove);
                NSLog(@"end time: %0f   interval: %0f", time/1000, (time - lasttime)/1000);
                if (bstart && !bmove)
                {
                    if (time - lasttime > 400)
                    {
                        NSLog(@"LongPress!!!!!!");
                    }
                    else
                    {
                        NSLog(@"Click!!!!!!");
                    }
                }
                bstart = NO;
                bmove = NO;
            }
            
            return NO;
        }
    }
    
    NSURL *url = [request URL];
    NSString *curUrl= [url absoluteString];
    NSLog(@"cururl: %@", curUrl);
    
    return result;
}


有时间再写个和微博类似的超文本显示的例子

参见点击打开链接

本资源集提供了针对小型无人机六自由度非线性动力学模型的MATLAB仿真环境,适用于多个版本(如2014a、2019b、2024b)。该模型完整描述了飞行器在三维空间中的六个独立运动状态:绕三个坐标轴的旋转(滚转、俯仰、偏航)与沿三个坐标轴的平移(前后、左右、升降)。建模过程严格依据牛顿-欧拉方程,综合考虑了重力、气动力、推进力及其产生的力矩对机体运动的影响,涉及矢量运算与常微分方程求解等数学方法。 代码采用模块化与参数化设计,使用者可便捷地调整飞行器的结构参数(包括几何尺寸、质量特性、惯性张量等)以匹配不同机型。程序结构清晰,关键步骤配有详细说明,便于理解模型构建逻辑与仿真流程。随附的示例数据集可直接加载运行,用户可通过修改参数观察飞行状态的动态响应,从而深化对无人机非线性动力学特性的认识。 本材料主要面向具备一定数学与编程基础的高校学生,尤其适合计算机、电子信息工程、自动化及相关专业人员在课程项目、专题研究或毕业设计中使用。通过该仿真环境,学习者能够将理论知识与数值实践相结合,掌握无人机系统建模、仿真与分析的基本技能,为后续从事飞行器控制、系统仿真等领域的研究或开发工作奠定基础。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值