Skip to content
This repository has been archived by the owner on Jul 24, 2021. It is now read-only.

Commit

Permalink
Add support for update text
Browse files Browse the repository at this point in the history
  • Loading branch information
Cokile committed Jun 18, 2016
1 parent 12be1c9 commit c15f745
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 31 deletions.
9 changes: 9 additions & 0 deletions CCActivityHUD/CCActivityHUD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
27 changes: 26 additions & 1 deletion CCActivityHUD/CCActivityHUD.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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];
Expand Down
66 changes: 36 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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`.
Expand All @@ -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]`.


Expand Down Expand Up @@ -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/).
Expand Down

0 comments on commit c15f745

Please sign in to comment.