Skip to content

Commit

Permalink
Maximize thread safety for showing.
Browse files Browse the repository at this point in the history
Signed-off-by: Zachary Waldowski <zwaldowski@gmail.com>
  • Loading branch information
zwaldowski committed Jan 31, 2012
1 parent 7924369 commit 705ed12
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 49 deletions.
34 changes: 16 additions & 18 deletions MBProgressHUD.h
Expand Up @@ -96,22 +96,27 @@ typedef enum {
@property (nonatomic, weak) void(^wasHiddenBlock)(MBProgressHUD *);

/*
* Grace period is the time (in seconds) that the invoked method may be run without
* showing the HUD. If the task finishes befor the grace time runs out, the HUD will
* not be shown at all.
* This may be used to prevent HUD display for very short tasks.
* Defaults to 0 (no grace time).
* Grace time functionality is only supported when the task status is known!
* @see taskInProgress
* The show delay is the time (in seconds) that your method may run without the HUD
* being shown. If the task finishes before the grace time runs out, the HUD will
* not appear at all, usually if you have a very short task.
*
* Defaults to 0. If you don't set one and still might have a short task,
* it is recommended to set a minimum show time instead.
*
* @see minimumShowTime
*/
@property (nonatomic) NSTimeInterval graceTime;
@property (nonatomic) NSTimeInterval showDelayTime;

/**
* 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).
*
* Defaults to 0.0. If you don't set one and your task might run short,
* it is recommended to instead set a show delay time.
*
* @see showDelayTime
*/
@property (nonatomic) NSTimeInterval minShowTime;
@property (nonatomic) NSTimeInterval minimumShowTime;

/**
* Removes the HUD from it's parent view when hidden.
Expand Down Expand Up @@ -140,14 +145,7 @@ typedef enum {
@property (nonatomic) CGSize minSize;

/**
* Display the HUD. You need to make sure that the main thread completes its run loop soon after this method call so
* the user interface can be updated. Call this method when your task is already set-up to be executed in a new thread
* (e.g., when using something like NSOperation or calling an asynchronous call like NSUrlRequest).
*
* If you need to perform a blocking thask on the main thread, you can try spining the run loop imeidiately after calling this
* method by using:
*
* [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantPast]];
* Display the HUD.
*
* @param animated If set to YES the HUD will disappear using the current animationType. If set to NO the HUD will not use
* animations while disappearing.
Expand Down
52 changes: 21 additions & 31 deletions MBProgressHUD.m
Expand Up @@ -40,16 +40,11 @@ @implementation MBProgressHUD {
#pragma mark Accessors

@synthesize mode;

@synthesize wasHiddenBlock;

@synthesize minSize;

@synthesize customView;

@synthesize graceTime, minShowTime;

@synthesize wasHiddenBlock;
@synthesize removeFromSuperViewOnHide;
@synthesize showDelayTime, minimumShowTime;

#pragma mark - Class methods

Expand Down Expand Up @@ -77,8 +72,6 @@ + (BOOL)hideHUDForView:(UIView *)view animated:(BOOL)animated {
}
}



#pragma mark - Internal notifications

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
Expand Down Expand Up @@ -181,10 +174,7 @@ - (void)dealloc {
[detailLabel removeObserver:self forKeyPath:@"textAlignment"];
}

#pragma mark -
#pragma mark Lifecycle methods


#pragma mark - Lifecycle methods

- (void)removeFromSuperview {
[super removeFromSuperview];
Expand All @@ -194,19 +184,19 @@ - (void)removeFromSuperview {
#pragma mark - Showing and hiding

- (void)show:(BOOL)animated {
[self reloadOrientation:nil];
self.alpha = 0.0f;
self.transform = CGAffineTransformConcat(_rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));

NSTimeInterval length = animated ? (1./3.) : 0;
NSTimeInterval graceTimeDelay = self.graceTime;

[UIView animateWithDuration:length delay:graceTimeDelay options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^{
self.transform = _rotationTransform;
self.alpha = 1.0f;
} completion:^(BOOL finished) {
_showStarted = [[NSDate date] timeIntervalSinceReferenceDate];
}];
dispatch_always_main_queue(^{
[self reloadOrientation:nil];
self.alpha = 0.0f;
self.transform = CGAffineTransformConcat(_rotationTransform, CGAffineTransformMakeScale(0.5f, 0.5f));
NSTimeInterval length = animated ? (1./3.) : 0;
[UIView animateWithDuration:length delay:self.showDelayTime options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction animations:^{
self.transform = _rotationTransform;
self.alpha = 1.0f;
} completion:^(BOOL finished) {
_showStarted = [[NSDate date] timeIntervalSinceReferenceDate];
}];
});
}

- (void)hide:(BOOL)animated {
Expand All @@ -220,13 +210,13 @@ - (void)hide:(BOOL)animated afterDelay:(NSTimeInterval)delay {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^{
NSTimeInterval length = animated ? (1./3.) : 0;
NSTimeInterval minimumShowDelay = 0.0;
NSTimeInterval delay = 0.0;
if (_showStarted) {
minimumShowDelay = self.minShowTime - ([[NSDate date] timeIntervalSinceReferenceDate] - _showStarted);
delay = minimumShowTime - ([[NSDate date] timeIntervalSinceReferenceDate] - _showStarted);
_showStarted = 0.0;
}

[UIView animateWithDuration:length delay:minimumShowDelay options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState animations:^{
[UIView animateWithDuration:length delay:delay options:UIViewAnimationOptionCurveEaseInOut|UIViewAnimationOptionAllowUserInteraction|UIViewAnimationOptionBeginFromCurrentState animations:^{
self.transform = CGAffineTransformConcat(_rotationTransform, CGAffineTransformMakeScale(1.5f, 1.5f));
self.alpha = 0.0f;
} completion:^(BOOL finished) {
Expand Down Expand Up @@ -280,7 +270,6 @@ - (void)drawRect:(CGRect)rect {
CGContextFillPath(context);
}


- (void)layoutSubviews {
CGRect frame = self.bounds, lFrame = CGRectZero, dFrame = CGRectZero, indFrame = indicator.bounds;
CGSize newSize = CGSizeMake(indFrame.size.width + 2 * margin, indFrame.size.height + 2 * margin);
Expand Down Expand Up @@ -448,8 +437,9 @@ - (UILabel *)detailLabel {
[newLabel addObserver:self forKeyPath:@"font" options:NSKeyValueObservingOptionNew context:&kDetailLabelContext];
[newLabel addObserver:self forKeyPath:@"textColor" options:NSKeyValueObservingOptionNew context:&kDetailLabelContext];
[newLabel addObserver:self forKeyPath:@"textAlignment" options:NSKeyValueObservingOptionNew context:&kDetailLabelContext];
[self addSubview:newLabel];
detailLabel = newLabel;
[self addSubview:newLabel];

}
return detailLabel;
}
Expand Down

0 comments on commit 705ed12

Please sign in to comment.