我正在xcode 5中开发iOS7的应用程序。我有一个视图控制器ADMSViewController
.h文件如下
#import <UIKit/UIKit.h>
#import "IMTWebView.h"
@interface ADMSViewController : UIViewController <UIWebViewDelegate,IMTWebViewProgressDelegate>
{
NSString *barcode;
NSString *vinNumber;
NSTimer *timer;
}
@property (nonatomic,retain) IBOutlet UIWebView *webView;
@property (nonatomic,retain) IBOutlet UIProgressView *progressView;
@end
.m文件如下
#import "ADMSViewController.h"
#import <QuartzCore/QuartzCore.h>
@implementation ADMSViewController
@synthesize webView;
@synthesize progressView;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com/"]]];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)webView:(IMTWebView *)_webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources {
[self.progressView setProgress:((float)resourceNumber) / ((float)totalResources)];
if (resourceNumber == totalResources) {
_webView.resourceCount = 0;
_webView.resourceCompletedCount = 0;
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
self.webView = nil;
self.progressView = nil;
}
@end
IMTWebview.h文件如下
#import <UIKit/UIKit.h>
@class IMTWebView;
@protocol IMTWebViewProgressDelegate <NSObject>
@required
- (void) webView:(IMTWebView*)webView didReceiveResourceNumber:(int)resourceNumber totalResources:(int)totalResources;
@end
@interface IMTWebView : UIWebView {
}
@property (nonatomic, assign) int resourceCount;
@property (nonatomic, assign) int resourceCompletedCount;
@property (nonatomic, assign) id<IMTWebViewProgressDelegate> progressDelegate;
@end
IMTWebCiew.m文件如下
#import "IMTWebView.h"
@interface UIWebView ()
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource;
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource;
-(void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource;
@end
@implementation IMTWebView
@synthesize progressDelegate;
@synthesize resourceCount;
@synthesize resourceCompletedCount;
-(id)webView:(id)view identifierForInitialRequest:(id)initialRequest fromDataSource:(id)dataSource
{
[super webView:view identifierForInitialRequest:initialRequest fromDataSource:dataSource];
return [NSNumber numberWithInt:resourceCount++];
}
- (void)webView:(id)view resource:(id)resource didFailLoadingWithError:(id)error fromDataSource:(id)dataSource {
[super webView:view resource:resource didFailLoadingWithError:error fromDataSource:dataSource];
resourceCompletedCount++;
if ([self.progressDelegate respondsToSelector:@selector(webView:didReceiveResourceNumber:totalResources:)]) {
[self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount];
}
}
-(void)webView:(id)view resource:(id)resource didFinishLoadingFromDataSource:(id)dataSource
{
[super webView:view resource:resource didFinishLoadingFromDataSource:dataSource];
resourceCompletedCount++;
if ([self.progressDelegate respondsToSelector:@selector(webView:didReceiveResourceNumber:totalResources:)]) {
[self.progressDelegate webView:self didReceiveResourceNumber:resourceCompletedCount totalResources:resourceCount];
}
}
@end
我的问题是进度条没有在webview中设置动画。请帮助我,因为我是ios开发的新手。如果有任何其他方法来实现相同的,请通知我。 提前感谢您的回答。
答案 0 :(得分:0)
对于这种事情,你只能通过使用Content-Length标题来“检测”页面大小,但是否则你仍然可以呈现一个加载控件(就像一个移动的斑马线型条形图,它没有显示离散%进度,但表明活动正在发生..甚至只是一个UIActivityIndicator)。您也可以尝试像http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest这样的现成课程,它可以显示进度。作者说:
ASIWebPageRequests目前不支持使用其外部资源对整个请求进行进度跟踪。但是,来自外部资源请求的进度更新会传递给您的代理,因此可以通过一些工作自行实现。