MBProgressHUD立即被隐藏。如何设置最短演出时间?

时间:2014-03-05 07:47:52

标签: ios objective-c

我想使用customView显示MBProgressHUD以向用户显示购买内容。在这里我尝试了什么

- (void)showInfoAlert:(int)clues {

    UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];
    hud.customView = [[[UIImageView alloc] initWithImage:
                      [UIImage imageNamed:@"tick.png"]] autorelease];
    hud.mode = MBProgressHUDModeCustomView;
    hud.dimBackground = YES;
    NSString* msg = (clues == 1) ? @"1 Clue added to yor account." : [NSString stringWithFormat:@"%d Clues added to yor account.", clues];
    hud.labelText = msg;
    [hud showAnimated:YES whileExecutingBlock:^(void){

        [NSThread sleepForTimeInterval:3];
    }];
}

hud显示的时间很短,并立即隐藏。我想让它出现最少3秒钟。

1 个答案:

答案 0 :(得分:3)

您正在寻找的属性是

/**
 * The minimum time (in seconds) that the HUD is shown. 
 * This avoids the problem of the HUD being shown and than instantly hidden.
 * Defaults to 0 (no minimum show time).
 */
@property (assign) float minShowTime;

MBProgressHud班。

添加

hud.minShowTime = 3.f;

你应该好好去。

编辑:您应该删除对-showAnimated:whileExecutingBlock:的调用,因为它会在后台队列中调度该块:这就是为什么它不会阻止您的HUD(由主控制)线程,像所有其他UI组件一样)。无论如何将主线程置于睡眠状态并不是一个好主意,因为它会导致糟糕的用户体验(如果长时间阻塞,系统可能会杀死你的应用程序)。

您只需使用简单的-show:

替换该调用即可