ios - MBProgressHUD 宽限时间和最小时间无法正常工作。如何实现。

标签 ios mbprogresshud

我正在使用 MBProgressHUD用于在执行本地长方法时显示加载,以提供良好的用户体验。一切正常,但现在我必须这样实现,如果该方法在一秒钟内执行,那么不应出现加载。我浏览了 MBProgressHUD 示例,对于此功能,我发现 setGraceTimesetMinShowTime 显示时间,但它们都无法正常工作。因为当我设置宽限时间时,即使方法执行时间超过 1 秒,加载图标也不会出现。这是我的代码

    if (self.HUD == nil)
    {
        self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
        [self.view addSubview:self.HUD];
    }
    self.HUD.labelText = @"Please wait.....";
    //        [self.HUD setMinShowTime:2.0f];

    [self.HUD setGraceTime:1.0f];
    [self.HUD setTaskInProgress:YES];
    [self.HUD show:YES];
    //        [self.HUD hide:YES afterDelay:3];

    [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];

    if (mycondition == nil)
    {
            [self myTask:[NSURL fileURLWithPath:strPath]];
            //[self performSelector:@selector(mytask:) withObject:[NSURL fileURLWithPath:strPath]];
    }
    else
    {
      //else
    }

    self.tempView.hidden = YES;
    //        self.HUD.taskInProgress = NO;
    [self.HUD hide:YES];
    self.HUD = nil;

我已经为 this problem's solution 设置了 [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]]; 请指导我这段代码有什么问题..?!

最佳答案

您需要在方法执行时从其父 View 中删除 MBProgressHUD 对象。 像这样:

// if the method runs inside one second, the indicator will not be shown

- (void)viewDidLoad
{
  [super viewDidLoad];
  if (HUD == nil)
  {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
  }
  HUD.labelText = @"Please wait.....";
  [HUD setGraceTime:1.0f];
  [HUD setTaskInProgress:YES];
  [HUD show:YES];
  [self performSelector:@selector(runThisMethod) withObject:nil afterDelay:0.9f];
}

- (void)runThisMethod
{
    [HUD removeFromSuperview];
}

// if the method runs after one second, the indicator will be shown for some time until the method runs

- (void)viewDidLoad
{
  [super viewDidLoad];
  if (HUD == nil)
  {
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
  }
  HUD.labelText = @"Please wait.....";
  [HUD setGraceTime:1.0f];
  [HUD setTaskInProgress:YES];
  [HUD show:YES];
  [self performSelector:@selector(runThisMethod) withObject:nil afterDelay:1.9f];
}

- (void)runThisMethod
{
    [HUD removeFromSuperview];
}

关于ios - MBProgressHUD 宽限时间和最小时间无法正常工作。如何实现。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18008708/

相关文章:

ios - 如何在 UIImagePickerController 上自定义 MBProgressHUD View ?

ios - 无法将自定义字体包含到 xcode 项目中

iphone - 如何使用两个后续的 MBProgressHUD 消息

iphone - MBProgressHud 不随设备方向改变。?

ios - SwiftSoup cocoapods 只安装并且只安装版本 1.4,而不是 1.5

ios - MBProgressHUD 无法覆盖键盘

ios - 使用带有 MBProgressHUD 的自定义动画

ios - Swift 中的 NSDate 排序

ios - ConvertPointFromView 返回 NAN

ios - 从 UITableView 中删除一个 UITableViewCell