iosobjective-cmbprogresshud

Can't display MBProgressHUD progress animation properly


I would like to display a hud while the content is loading (and show the progress), but unfortunately it doesn't work properly. The hud appears on the screen when statusUpdate is 0.100000, but the loading bar doesn't moves until statusUpdate is not 1.000000 and the page loading finished. (After the view loaded sucessfully it animates from 0-100%.)

What am I doing wrong?

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

Solution

  • Unless I'm missing something, the problem is on - (void) hud: (double)statusUpdate

    For some reason you're casting the value (statusUpdate which is a double) to int and then again to a float, which means that 0.x values become 0.0 and 1.x values become 1.0 (that's why these are the only values that HUD is getting - since your range is 0.0/1.0)

    A simple fix would be something like this:

    - (void) hud: (double)statusUpdate  {
    
        NSLog(@"STATUS %f", statusUpdate);
    
        HUD.progress = statusUpdate;
    
    }