无法正确显示MBProgressHUD进度动画

时间:2015-08-01 22:49:52

标签: ios objective-c mbprogresshud

我想在内容加载时显示一个hud(并显示进度),但遗憾的是它无法正常工作。当0.100000statusUpdate时,屏幕上会显示hud,但加载栏不会移动,直到1.000000不是// 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; } 并且页面加载完成。 (视图加载成功后,它的动画从0-100%。)如果有人能告诉我我做错了什么,我将不胜感激。

<form action="/missions/basic/3/index.php" method="post">
<input type="hidden" name="file" value="password.php" />
<input type="password" name="password" /><br /><br />
<input type="submit" value="submit" />
<input type=button value="Get Value" onclick="printIt()" /></form>

3 个答案:

答案 0 :(得分:4)

除非我遗漏了某些内容,否则问题出在- (void) hud: (double)statusUpdate

出于某种原因,您将值{statusUpdatedouble)投射到int,然后再投射到float,这意味着{{1} }}值变为0.x0.0值变为1.x(这就是为什么这些是HUD获得的唯一值 - 因为您的范围是0.0 / 1.0)

一个简单的解决方法是这样的:

1.0

答案 1 :(得分:0)

以下是我以前如何使用它:

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;
    }];

答案 2 :(得分:0)

也许您可以使用CADisplayLink创建类似动画的内容。它会更新您的HUD。

@interface ViewController () {
    CADisplayLink *displayLink;
}
- (void)displayLinkMethod {    
    displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(animationMethod)];
        [displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
}