ios 无法正确显示MBProgressHUD进度动画

qlfbtfca  于 2023-08-08  发布在  iOS
关注(0)|答案(3)|浏览(145)

我想在内容加载时显示一个HUD(并显示进度),但不幸的是它不能正常工作。当statusUpdate0.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;
    
}

字符串

taor4pac

taor4pac1#

除非我遗漏了什么,否则问题出在- (void) hud: (double)statusUpdate
出于某种原因,您将值(statusUpdate,即double)转换为int,然后再次转换为float,这意味着0.x值变为0.01.x值变为1.0(这就是为什么HUD只获取这些值-因为您的范围是0.0/1.0)
一个简单的修复方法是这样的:

- (void) hud: (double)statusUpdate  {

    NSLog(@"STATUS %f", statusUpdate);

    HUD.progress = statusUpdate;

}

字符串

ndh0cuux

ndh0cuux2#

以下是我以前使用的方法:

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

字符串

mmvthczy

mmvthczy3#

也许你可以使用CADisplayLink创建类似动画的东西。它将更新您的HUD。

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

字符串

相关问题