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. - (void)webViewDidFinishLoad:(UIWebView *)webView { 
  33.      [activityIndicator stopAnimating];    
  34.      UIView *view = (UIView *)[self.view viewWithTag:103]; 
  35.      [view removeFromSuperview]; 

第二种方法:使用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. - (void)webViewDidFinishLoad:(UIWebView *)webView{ 
  18.       [myAlert dismissWithClickedButtonIndex:0 animated:YES]; 

来源: 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. -(void)hideWaiting 
  10.     [progressInd stopAnimating]; 

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

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

来源: http://blog.csdn.net/lovenjoe/article/details/7498238

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值