diff --git a/JGProgressHUD/JGProgressHUD/JGProgressHUD.h b/JGProgressHUD/JGProgressHUD/JGProgressHUD.h index 2338c0a..511b9dc 100755 --- a/JGProgressHUD/JGProgressHUD/JGProgressHUD.h +++ b/JGProgressHUD/JGProgressHUD/JGProgressHUD.h @@ -251,6 +251,13 @@ typedef NS_ENUM(NSUInteger, JGProgressHUDInteractionType) { - (void)setProgress:(float)progress animated:(BOOL)animated; +/** + Specify a minimum time that the HUD will be on-screen. Useful to prevent the HUD from flashing quickly on the screen when network operations complete more quickly than expected. + + @b Default: 0.0. + */ +@property (nonatomic, assign) NSTimeInterval minimumDisplayTime; + ///////////// // Showing // ///////////// diff --git a/JGProgressHUD/JGProgressHUD/JGProgressHUD.m b/JGProgressHUD/JGProgressHUD/JGProgressHUD.m index 1dee300..05db45f 100755 --- a/JGProgressHUD/JGProgressHUD/JGProgressHUD.m +++ b/JGProgressHUD/JGProgressHUD/JGProgressHUD.m @@ -42,6 +42,8 @@ @interface JGProgressHUD () { BOOL _dismissAfterTransitionFinished; BOOL _dismissAfterTransitionFinishedWithAnimation; + CFAbsoluteTime _displayTime; + JGProgressHUDIndicatorView *_indicatorViewAfterTransitioning; } @@ -328,6 +330,7 @@ - (void)cleanUpAfterPresentation { self.hidden = NO; _transitioning = NO; + _displayTime = CFAbsoluteTimeGetCurrent(); if (_indicatorViewAfterTransitioning) { self.indicatorView = _indicatorViewAfterTransitioning; @@ -389,6 +392,8 @@ - (void)showInRect:(CGRect)rect inView:(UIView *)view animated:(BOOL)animated { _transitioning = YES; + _displayTime = 0; + if ([self.delegate respondsToSelector:@selector(progressHUD:willPresentInView:)]) { [self.delegate progressHUD:self willPresentInView:view]; } @@ -435,6 +440,15 @@ - (void)dismissAnimated:(BOOL)animated { return; } + if (self.minimumDisplayTime > 0 && _displayTime > 0) { + CFAbsoluteTime displayedTime = CFAbsoluteTimeGetCurrent() - _displayTime; + if (displayedTime < self.minimumDisplayTime) { + NSTimeInterval delay = self.minimumDisplayTime - displayedTime; + [self dismissAfterDelay:delay animated:animated]; + return; + } + } + _transitioning = YES; if ([self.delegate respondsToSelector:@selector(progressHUD:willDismissFromView:)]) {