首页 - IOS编程

UIWebView加载Loading...两种方法

第一种方法:使用UIView and UIActivityIndicatorView

  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

  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. }  
下一篇:IOS 之UIImageView