Implementation of Loading by UIWebView in IOS
Recently, a friend asked me how to load the loading interface with horn animation and interface image similar to voice playback. Is it a GIF image! Of course, my answer is no. Of course, there are also GIF images!
Method 1: Use UIView and UIActivityIndicatorView
The Code is as follows:
// Create a UIWebView
WebView = [[UIWebView alloc] initWithFrame: CGRectMake (0, 44,320,400)];
[WebView setUserInteractionEnabled: NO];
[WebView setBackgroundColor: [UIColor clearColor];
[WebView setDelegate: self];
[WebView setOpaque: NO]; // make the webpage transparent
NSString * path = @ "http://www.baidu.com ";
NSURL * url = [NSURL URLWithString: path];
[WebView loadRequest: [NSURLRequest requestWithURL: url];
// Create a UIActivityIndicatorView with a translucent background
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];
// Start loading data
-(Void) webViewDidStartLoad :( UIWebView *) webView {
[ActivityIndicator startAnimating];
}
// Data loaded
-(Void) webViewDidFinishLoad :( UIWebView *) webView {
[ActivityIndicator stopAnimating];
UIView * view = (UIView *) [self. view viewWithTag: 103];
[View removeFromSuperview];
}
Method 2: Use UIAlertView and UIActivityIndicatorView
The Code is as follows:
// Load webpage Animation
-(Void) webViewDidStartLoad :( UIWebView *) webView {
If (myAlert = nil ){
MyAlert = [[UIAlertView alloc] initWithTitle: nil
Message: @ "reading ..."
Delegate: self
CancelButtonTitle: nil
OtherButtonTitles: nil];
UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle: UIActivityIndicatorViewStyleWhite];
ActivityView. frame = CGRectMake (120.f, 48366f, 38366f, 38366f );
[MyAlert addSubview: activityView];
[ActivityView startAnimating];
[MyAlert show];
}
}
-(Void) webViewDidFinishLoad :( UIWebView *) webView {
[MyAlert dismissWithClickedButtonIndex: 0 animated: YES];
}
Method 3: Use UIWebView to load GIF images. Do not use this method unless you use webView.
The Code is as follows:
NSData * gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ "1" ofType: @ "gif"];
// View generation
UIWebView * webView = [[UIWebView alloc] initWithFrame: CGRectMake (100,100, 70, 30)];
WebView. userInteractionEnabled = NO; // the user cannot interact.
[WebView loadData: gif MIMEType: @ "image/gif" textEncodingName: nil baseURL: nil];
[Self. view addSubview: webView];
The above is all the content of this article. I hope you will like it.