欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS中UIWebView加载Loading的实现方法

程序员文章站 2022-03-02 15:06:07
第一种方法:使用uiview and uiactivityindicatorview 复制代码 代码如下: //创建uiwebview webview = [[uiw...

第一种方法:使用uiview and uiactivityindicatorview

复制代码 代码如下:

//创建uiwebview
webview = [[uiwebview alloc] initwithframe:cgrectmake(0, 44, 320, 400)];
[webview setuserinteractionenabled:no];
[webview setbackgroundcolor:[uicolor clearcolor]];
[webview setdelegate:self];
[webview setopaque:no];//使网页透明
nsstring *path = @"";
nsurl *url = [nsurl urlwithstring:path];
[webview loadrequest:[nsurlrequest requestwithurl:url]];
//创建uiactivityindicatorview背底半透明view  
uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, 320, 480)];
[view settag:103];
[view setbackgroundcolor:[uicolor blackcolor]];
[view setalpha:0.8];
[self.view addsubview:view];
activityindicator = [[uiactivityindicatorview alloc] initwithframe:cgrectmake(0.0f, 0.0f, 32.0f, 32.0f)];
[activityindicator setcenter:view.center];
[activityindicator setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite];
[view addsubview:activityindicator];
[self.view addsubview:webview];
[view release];
[webview release];
//开始加载数据
- (void)webviewdidstartload:(uiwebview *)webview {  
      [activityindicator startanimating];       
}
//数据加载完
- (void)webviewdidfinishload:(uiwebview *)webview {
     [activityindicator stopanimating];  
     uiview *view = (uiview *)[self.view viewwithtag:103];
     [view removefromsuperview];
}

第二种方法:使用uialertview and uiactivityindicatorview

复制代码 代码如下:

//加载网页动画
- (void)webviewdidstartload:(uiwebview *)webview{
    if (myalert==nil){      
       myalert = [[uialertview alloc] initwithtitle:nil
                                                              message: @"读取中..."
                                                                delegate: self
                                                 cancelbuttontitle: nil
                                                 otherbuttontitles: nil];
     uiactivityindicatorview *activityview = [[uiactivityindicatorview alloc] initwithactivityindicatorstyle:uiactivityindicatorviewstylewhite];
     activityview.frame = cgrectmake(120.f, 48.0f, 38.0f, 38.0f);
     [myalert addsubview:activityview];
     [activityview startanimating];
     [myalert show];
     }
}
- (void)webviewdidfinishload:(uiwebview *)webview{
      [myalert dismisswithclickedbuttonindex:0 animated:yes];
}

方法三:使用uiwebview来加载gif图片,除非你要用到webview,不然就不要使用这种方式来实现

复制代码 代码如下:

nsdata *gif = [nsdata datawithcontentsoffile: [[nsbundle mainbundle] pathforresource:@"1" oftype:@"gif"]]; 
// view生成 
uiwebview *webview = [[uiwebview alloc] initwithframe:cgrectmake(100, 100, 70, 30)]; 
webview.userinteractionenabled = no;//用户不可交互 
[webview loaddata:gif mimetype:@"image/gif" textencodingname:nil baseurl:nil]; 
[self.view addsubview:webview]; 

以上所述就是本文的全部内容了,希望大家能够喜欢。