Skip to content

Commit

Permalink
Suppose you show a HUD during a network request, but that request com…
Browse files Browse the repository at this point in the history
…pletes very quickly. Without this feature, the HUD appears and then quickly disappears, flashing too quickly on the screen. Prevent quick flashes by allowing specification of a “minimumDisplayTime”.
  • Loading branch information
mallorypaine committed Feb 16, 2015
1 parent 4f007cd commit 2262053
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions JGProgressHUD/JGProgressHUD/JGProgressHUD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
/////////////
Expand Down
14 changes: 14 additions & 0 deletions JGProgressHUD/JGProgressHUD/JGProgressHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ @interface JGProgressHUD () {
BOOL _dismissAfterTransitionFinished;
BOOL _dismissAfterTransitionFinishedWithAnimation;

CFAbsoluteTime _displayTime;

JGProgressHUDIndicatorView *_indicatorViewAfterTransitioning;
}

Expand Down Expand Up @@ -328,6 +330,7 @@ - (void)cleanUpAfterPresentation {
self.hidden = NO;

_transitioning = NO;
_displayTime = CFAbsoluteTimeGetCurrent();

if (_indicatorViewAfterTransitioning) {
self.indicatorView = _indicatorViewAfterTransitioning;
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -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:)]) {
Expand Down

0 comments on commit 2262053

Please sign in to comment.