From 226205305b72f952c9ccc47f06f5fb80dda4ea4d Mon Sep 17 00:00:00 2001 From: Mallory Paine Date: Sun, 15 Feb 2015 21:11:05 -0800 Subject: [PATCH] =?UTF-8?q?Suppose=20you=20show=20a=20HUD=20during=20a=20n?= =?UTF-8?q?etwork=20request,=20but=20that=20request=20completes=20very=20q?= =?UTF-8?q?uickly.=20Without=20this=20feature,=20the=20HUD=20appears=20and?= =?UTF-8?q?=20then=20quickly=20disappears,=20flashing=20too=20quickly=20on?= =?UTF-8?q?=20the=20screen.=20Prevent=20quick=20flashes=20by=20allowing=20?= =?UTF-8?q?specification=20of=20a=20=E2=80=9CminimumDisplayTime=E2=80=9D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- JGProgressHUD/JGProgressHUD/JGProgressHUD.h | 7 +++++++ JGProgressHUD/JGProgressHUD/JGProgressHUD.m | 14 ++++++++++++++ 2 files changed, 21 insertions(+) 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:)]) {