UIWebView加载Loading...两种方法

第一种方法:使用UIView and UIActivityIndicatorView

C代码   收藏代码
  1. //创建UIWebView  
  2. WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 44, 320, 400)];  
  3. [WebView setUserInteractionEnabled:NO];  
  4. [WebView setBackgroundColor:[UIColor clearColor]];  
  5. [WebView setDelegate:self];  
  6. [WebView setOpaque:NO];//使网页透明  
  7.    
  8. NSString *path = @"http://www.baidu.com";  
  9. NSURL *url = [NSURL URLWithString:path];  
  10. [WebView loadRequest:[NSURLRequest requestWithURL:url]];  
  11.        
  12. //创建UIActivityIndicatorView背底半透明View     
  13. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];  
  14. [view setTag:103];  
  15. [view setBackgroundColor:[UIColor blackColor]];  
  16. [view setAlpha:0.8];  
  17. [self.view addSubview:view];  
  18.    
  19. activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];  
  20. [activityIndicator setCenter:view.center];  
  21. [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];  
  22. [view addSubview:activityIndicator];  
  23. [self.view addSubview:WebView];  
  24. [view release];  
  25. [WebView release];  
  26.    
  27. //开始加载数据  
  28. - (void)webViewDidStartLoad:(UIWebView *)webView {     
  29.       [activityIndicator startAnimating];          
  30. }  
  31.    
  32. //数据加载完  
  33. - (void)webViewDidFinishLoad:(UIWebView *)webView {  
  34.      [activityIndicator stopAnimating];     
  35.      UIView *view = (UIView *)[self.view viewWithTag:103];  
  36.      [view removeFromSuperview];  
  37. }  
 

第二种方法:使用UIAlertView and UIActivityIndicatorView

C代码   收藏代码
  1. //加载网页动画  
  2. - (void)webViewDidStartLoad:(UIWebView *)webView{  
  3.     if (myAlert==nil){         
  4.        myAlert = [[UIAlertView alloc] initWithTitle:nil  
  5.                                                               message: @"正在讀取網路資料"  
  6.                                                                 delegate: self  
  7.                                                  cancelButtonTitle: nil  
  8.                                                  otherButtonTitles: nil];  
  9.        
  10.      UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];  
  11.      activityView.frame = CGRectMake(120.f, 48.0f, 37.0f, 37.0f);  
  12.      [myAlert addSubview:activityView];  
  13.      [activityView startAnimating];  
  14.      [myAlert show];  
  15.      }  
  16. }  
  17.    
  18. - (void)webViewDidFinishLoad:(UIWebView *)webView{  
  19.       [myAlert dismissWithClickedButtonIndex:0 animated:YES];  
  20. }  
 

来源: http://www.cocoachina.com/bbs/read.php?tid=9419

 

用法一:只显示不停旋转的进度滚轮指示器。

C代码   收藏代码
  1. //显示进度滚轮指示器  
  2. -(void)showWaiting {  
  3.     progressInd=[[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhiteLarge];  
  4.     progressInd.center=CGPointMake(self.view.center.x,240);  
  5.     [self.navigationController.view addSubview:progressInd];  
  6.     [progressInd startAnimating];  
  7. }  
  8.   
  9. //消除滚动轮指示器  
  10. -(void)hideWaiting  
  11. {  
  12.     [progressInd stopAnimating];  
  13. }  
 

用法二:带有半透明背景的进度轮指示器。

C代码   收藏代码
  1. //显示进度滚轮指示器  
  2. -(void)showWaiting:(UIView *)parent  
  3. {  
  4.     int width = 32, height = 32;  
  5.     CGRect frame = CGRectMake(100, 200, 110, 70) ;//[parent frame]; //[[UIScreen mainScreen] applicationFrame];  
  6.     int x = frame.size.width;  
  7.     int y = frame.size.height;  
  8.     
  9.     frame = CGRectMake((x - width) / 2, (y - height) / 2, width, height);  
  10.     UIActivityIndicatorView* progressInd = [[UIActivityIndicatorView alloc]initWithFrame:frame];  
  11.     [progressInd startAnimating];  
  12.     progressInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;  
  13.   
  14.     frame = CGRectMake((x - 70)/2, (y - height) / 2 + height, 80, 20);  
  15.     UILabel *waitingLable = [[UILabel alloc] initWithFrame:frame];  
  16.     waitingLable.text = @"Loading...";  
  17.     waitingLable.textColor = [UIColor whiteColor];  
  18.     waitingLable.font = [UIFont systemFontOfSize:15];  
  19.     waitingLable.backgroundColor = [UIColor clearColor];  
  20.   
  21.     frame =  CGRectMake(100, 200, 110, 70) ;//[parent frame];  
  22.     UIView *theView = [[UIView alloc] initWithFrame:frame];  
  23.     theView.backgroundColor = [UIColor blackColor];  
  24.     theView.alpha = 0.7;  
  25.   
  26.     [theView addSubview:progressInd];  
  27.     [theView addSubview:waitingLable];  
  28.   
  29.     [progressInd release];  
  30.     [waitingLable release];  
  31.   
  32.     [theView setTag:9999];  
  33.     [parent addSubview:theView];  
  34.     [theView release];  
  35.   
  36. }  
  37.   
  38.   
  39. //消除滚动轮指示器  
  40. -(void)hideWaiting  
  41.   
  42. {  
  43.     [[self.view viewWithTag:9999] removeFromSuperview];  
  44.   
  45. }  

来源: http://blog.csdn.net/lovenjoe/article/details/7498238
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值