Ios Can';t正确显示MBProgressHUD进度动画
我想在加载内容时显示hud(并显示进度),但不幸的是,它不能正常工作。当Ios Can';t正确显示MBProgressHUD进度动画,ios,objective-c,mbprogresshud,Ios,Objective C,Mbprogresshud,我想在加载内容时显示hud(并显示进度),但不幸的是,它不能正常工作。当statusUpdate为0.100000时,hud显示在屏幕上,但加载栏在statusUpdate未为1.000000且页面加载完成之前不会移动。(成功加载视图后,它从0-100%进行动画制作)如果有人能告诉我我做错了什么,我将不胜感激 // ViewDidLoad [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKe
statusUpdate
为0.100000
时,hud显示在屏幕上,但加载栏在statusUpdate
未为1.000000
且页面加载完成之前不会移动。(成功加载视图后,它从0-100%进行动画制作)如果有人能告诉我我做错了什么,我将不胜感激
// ViewDidLoad
[self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;
HUD.delegate = self;
HUD.labelText = @"Uploading";
[HUD show:YES];
[self hud:self.webView.estimatedProgress];
if ([keyPath isEqualToString:@"estimatedProgress"] && object == self.webView) {
// [self.progressView setAlpha:1.0f];
// [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
NSLog(@"%f", self.webView.estimatedProgress);
}
else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
NSLog(@"%f", self.webView.estimatedProgress);
}
}
- (void) hud: (double)statusUpdate {
NSLog(@"STATUS %f", statusUpdate);
int myInt = (int)statusUpdate;
HUD.progress = (float)myInt;
}
以下是我以前是如何使用它的:
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController;
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:vc.view animated:YES];
hud.mode = MBProgressHUDModeAnnularDeterminate;
hud.progress = 0;
hud.labelText = NSLocalizedString(@"Loading...", nil);
[operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
CGFloat progressValue = ((CGFloat)totalBytesRead/(CGFloat)totalBytesExpectedToRead);
MBProgressHUD *hud = [MBProgressHUD HUDForView:vc.view];
hud.progress = progressValue;
}];
除非我遗漏了什么,否则问题出在
-(void)hud:(double)statusUpdate
出于某种原因,您正在将值(statusUpdate
是一个double
)转换为int
,然后再次转换为float
,这意味着0.x
值变为0.0
,而1.x
值变为1.0
(这就是为什么HUD只能获得这些值——因为您的范围是0.0/1.0)
简单的修复方法如下所示:
- (void) hud: (double)statusUpdate {
NSLog(@"STATUS %f", statusUpdate);
HUD.progress = statusUpdate;
}
也许你可以使用CADisplayLink创建一些类似动画的东西。它会更新你的HUD
@interface ViewController () {
CADisplayLink *displayLink;
}
- (void)displayLinkMethod {
displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animationMethod)];
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}