diff --git a/CCActivityHUD/CCActivityHUD.h b/CCActivityHUD/CCActivityHUD.h index a129233..1740a33 100644 --- a/CCActivityHUD/CCActivityHUD.h +++ b/CCActivityHUD/CCActivityHUD.h @@ -142,6 +142,15 @@ typedef NS_ENUM(NSInteger, CCActivityHUDOverlayType) { */ - (void)showWithText:(NSString *)text shimmering:(BOOL)shimmering; + +/** + * Update the text shown by showWithText:shimmering: method. + * + * @param text The text to be updated. + * @param shimmering A boolean indicates whether apply a shimmering effect to the text. + */ +- (void)updateText:(NSString *)text shimmering:(BOOL)shimmering; + /** * Show a HUD with a circle progress bar. */ diff --git a/CCActivityHUD/CCActivityHUD.m b/CCActivityHUD/CCActivityHUD.m index acabdd3..70349a7 100644 --- a/CCActivityHUD/CCActivityHUD.m +++ b/CCActivityHUD/CCActivityHUD.m @@ -146,7 +146,7 @@ - (void)showWithText:(NSString *)text shimmering:(BOOL)shimmering{ CGFloat height = [self heightForText:text]+8; self.frame = CGRectMake(0, -height, TEXT_WIDTH, height); - UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ViewFrameWidth, ViewFrameHeight)]; + UILabel *textLabel = [[UILabel alloc] initWithFrame:self.bounds]; textLabel.numberOfLines = 0; textLabel.font = [UIFont systemFontOfSize:TEXT_FONT_SIZE]; textLabel.textColor = [self inverseColorFor:self.backgroundColor]; @@ -165,6 +165,31 @@ - (void)showWithText:(NSString *)text shimmering:(BOOL)shimmering{ } } +- (void)updateText:(NSString *)text shimmering:(BOOL)shimmering { + if (self.superview) { + [self removeAllSubviews]; + + self.frame = CGRectMake(0, 0, TEXT_WIDTH, [self heightForText:text]+8); + self.center = BoundsCenterFor(Screen); + + // addShimmeringEffectForLabel: invoked within showWithText:shimmering: will affect the layer of the UILabel + // So I can not use the same UILabel + UILabel *textLabel = [[UILabel alloc] initWithFrame:self.bounds]; + textLabel.numberOfLines = 0; + textLabel.font = [UIFont systemFontOfSize:TEXT_FONT_SIZE]; + textLabel.textColor = [self inverseColorFor:self.backgroundColor]; + textLabel.textAlignment = NSTextAlignmentCenter; + textLabel.text = text; + [self addSubview:textLabel]; + + if (shimmering) { + [self addShimmeringEffectForLabel:textLabel]; + } + + [UIView transitionWithView:self duration:0.3 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:nil]; + } +} + - (void)showWithProgress { if (!self.superview) { [self initializeReplicatorLayer]; diff --git a/README.md b/README.md index e9d611b..8022685 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // When the task has completed. [self.activityHUD dismiss]; }); - }); +}); ``` #### More options @@ -71,7 +71,7 @@ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // When the task has completed. [self.activityHUD dismiss]; }); - }); +}); ``` __Note:__ You should not call `showWithType:` within `viewDidLoad`, The Animation will not work! Instead, show it within `viewWillAppear` or `viewDidAppear`. @@ -86,22 +86,28 @@ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // When the task has completed. [self.activityHUD dismiss]; }); - }); +}); ``` -- Or just want to show some text to users? `CCActivityHUD` also supports showing text, even with shimmering visual effect. +- Or just want to show some text to users? `CCActivityHUD` also supports showing text, even with shimmering visual effect. More over, you can also update the text to tell user the current state of the task. ```objective-c [self.activityHUD showWithText:@"Now loading..." shimmering:YES]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ - // Your task code here + // Your tasks code here. + // ... + dispatch_async(dispatch_get_main_queue(), ^{ + // When part of the task has completed. + [self.activityHUD updateWithText:@"step #1 completed" shimmering:YES]; + }); + // You tasks code here. + // ... dispatch_async(dispatch_get_main_queue(), ^{ - // When the task has completed. + // When all the tasks have completed. [self.activityHUD dismiss]; }); - }); +}); ``` - - `CCActivityHUD` also support progress. Use `showWithProgress` and perform your task code. In the call back method, update UI in the main thread by `self.activityHUD.progress = completedTaskAmount/totalTaskAmount`, When all the tasks have completed, use`[self.activityHUD dismiss]`. @@ -205,28 +211,28 @@ From v.2.1.1, You can customize the indicator shape and indicator animation. ```objective-c [self.activityHUD showWithShape:^(CAShapeLayer *shapeLayer, CAReplicatorLayer *layer) { - shapeLayer.frame = CGRectMake(0, 0, 20, 20); - shapeLayer.position = CGPointMake(self.activityHUD.frame.size.width/2, self.activityHUD.frame.size.height/2); - shapeLayer.backgroundColor = [UIColor whiteColor].CGColor; - shapeLayer.cornerRadius = 10; - } animationGroup:^(CAAnimationGroup *animationGroup) { - CABasicAnimation *animation1 = [[CABasicAnimation alloc] init]; - animation1.keyPath = @"transform.scale"; - animation1.fromValue = [NSNumber numberWithFloat:1.0]; - animation1.toValue = [NSNumber numberWithFloat:0.5]; - animation1.duration = 2.0; - - CABasicAnimation *animation2 = [[CABasicAnimation alloc] init]; - animation2.keyPath = @"transform.scale"; - animation2.beginTime = 2.0; - animation2.fromValue = [NSNumber numberWithFloat:0.5]; - animation2.toValue = [NSNumber numberWithFloat:1.0]; - animation2.duration = 2.0; - - animationGroup.duration = 4.0; - animationGroup.repeatCount = INFINITY; - animationGroup.animations = @[animation1, animation2]; - }]; + shapeLayer.frame = CGRectMake(0, 0, 20, 20); + shapeLayer.position = CGPointMake(self.activityHUD.frame.size.width/2, self.activityHUD.frame.size.height/2); + shapeLayer.backgroundColor = [UIColor whiteColor].CGColor; + shapeLayer.cornerRadius = 10; +} animationGroup:^(CAAnimationGroup *animationGroup) { + CABasicAnimation *animation1 = [[CABasicAnimation alloc] init]; + animation1.keyPath = @"transform.scale"; + animation1.fromValue = [NSNumber numberWithFloat:1.0]; + animation1.toValue = [NSNumber numberWithFloat:0.5]; + animation1.duration = 2.0; + + CABasicAnimation *animation2 = [[CABasicAnimation alloc] init]; + animation2.keyPath = @"transform.scale"; + animation2.beginTime = 2.0; + animation2.fromValue = [NSNumber numberWithFloat:0.5]; + animation2.toValue = [NSNumber numberWithFloat:1.0]; + animation2.duration = 2.0; + + animationGroup.duration = 4.0; + animationGroup.repeatCount = INFINITY; + animationGroup.animations = @[animation1, animation2]; +}]; ``` * `shapeLayer` is used to draw indicator shape. You can use it with `UIBezierPath` draw more complicated shape. For more details about `CAShapeLayer` and `UIBezierPath` you can visit [ CAShapeLayer Class Reference](https://developer.apple.com/library/ios/documentation/GraphicsImaging/Reference/CAShapeLayer_class/) and [UIBezierPath Class Reference](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBezierPath_class/).