IOS中UIWebView加载Loading的实现方法
第一种方法:使用UIViewandUIActivityIndicatorView
//创建UIWebView WebView=[[UIWebViewalloc]initWithFrame:CGRectMake(0,44,320,400)]; [WebViewsetUserInteractionEnabled:NO]; [WebViewsetBackgroundColor:[UIColorclearColor]]; [WebViewsetDelegate:self]; [WebViewsetOpaque:NO];//使网页透明 NSString*path=@"http://www.baidu.com"; NSURL*url=[NSURLURLWithString:path]; [WebViewloadRequest:[NSURLRequestrequestWithURL:url]]; //创建UIActivityIndicatorView背底半透明View UIView*view=[[UIViewalloc]initWithFrame:CGRectMake(0,0,320,480)]; [viewsetTag:103]; [viewsetBackgroundColor:[UIColorblackColor]]; [viewsetAlpha:0.8]; [self.viewaddSubview:view]; activityIndicator=[[UIActivityIndicatorViewalloc]initWithFrame:CGRectMake(0.0f,0.0f,32.0f,32.0f)]; [activityIndicatorsetCenter:view.center]; [activityIndicatorsetActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite]; [viewaddSubview:activityIndicator]; [self.viewaddSubview:WebView]; [viewrelease]; [WebViewrelease]; //开始加载数据 -(void)webViewDidStartLoad:(UIWebView*)webView{ [activityIndicatorstartAnimating]; } //数据加载完 -(void)webViewDidFinishLoad:(UIWebView*)webView{ [activityIndicatorstopAnimating]; UIView*view=(UIView*)[self.viewviewWithTag:103]; [viewremoveFromSuperview]; }
第二种方法:使用UIAlertViewandUIActivityIndicatorView
//加载网页动画 -(void)webViewDidStartLoad:(UIWebView*)webView{ if(myAlert==nil){ myAlert=[[UIAlertViewalloc]initWithTitle:nil message:@"读取中..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; UIActivityIndicatorView*activityView=[[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; activityView.frame=CGRectMake(120.f,48.0f,38.0f,38.0f); [myAlertaddSubview:activityView]; [activityViewstartAnimating]; [myAlertshow]; } } -(void)webViewDidFinishLoad:(UIWebView*)webView{ [myAlertdismissWithClickedButtonIndex:0animated:YES]; }
方法三:使用UIWebView来加载gif图片,除非你要用到webView,不然就不要使用这种方式来实现
NSData*gif=[NSDatadataWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"1"ofType:@"gif"]]; //view生成 UIWebView*webView=[[UIWebViewalloc]initWithFrame:CGRectMake(100,100,70,30)]; webView.userInteractionEnabled=NO;//用户不可交互 [webViewloadData:gifMIMEType:@"image/gif"textEncodingName:nilbaseURL:nil]; [self.viewaddSubview:webView];
以上所述就是本文的全部内容了,希望大家能够喜欢。