从 v2.0.0 版本开始,我将它从 CCActivityIndicatorView 重命名为 CCActivityHUD。如果您在我重命名之前分叉了项目,您应该运行以下命令来正确地提交一个 Pull Request。
git remote set-url upstream [email protected]:Cokile/CCActivityHUD.git
将 CCActivityHUD 文件夹中的所有文件添加到您的项目中。
#import <CCActivityHUD/CCActivityHUD.h>
self.activityHUD = [CCActivityIndicatorView new];
// Show with the default type.
[self.myactivityHUD show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Your task code here
dispatch_async(dispatch_get_main_queue(), ^{
// When the task has completed.
[self.activityHUD dismiss];
});
});
show
显示默认指示器动画类型,也可以指定要显示的类型(要查看您可以使用的类型,请参阅指示器动画类型部分)。[self.activityHUD showWithType:CCActivityHUDIndicatorTypeDynamicArc];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Your task code here
dispatch_async(dispatch_get_main_queue(), ^{
// When the task has completed.
[self.activityHUD dismiss];
});
});
注意:您不应在 viewDidLoad
中调用 showWithType:
,动画将不会工作!相反,您应该在 viewWillAppear
或 viewDidAppear
中显示它。
CCActivityHUD
也支持 GIF。只需找一个您喜欢的 GIF,并用它来显示。**(记得将 ImageIO.framework
添加到您的目标)**[self.activityHUD showWithGIFName:@"test.gif"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Your task code here
dispatch_async(dispatch_get_main_queue(), ^{
// When the task has completed.
[self.activityHUD dismiss];
});
});
CCActivityHUD
也支持显示文本,即使带有闪烁的视觉效果。此外,您还可以更新文本,以便用户了解任务当前的状态。[self.activityHUD showWithText:@"Now loading..." shimmering:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 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 all the tasks have completed.
[self.activityHUD dismiss];
});
});
CCActivityHUD
还支持进度。使用 showWithProgress
执行任务代码。在回调方法中,通过 self.activityHUD.progress = completedTaskAmount/totalTaskAmount
在主线程中更新 UI,当所有任务都完成后,使用 [self.activityHUD dismiss]
。
最后但并非最不重要的是,当任务完成后,有时您需要向用户显示一些文本作为反馈,而不是简单地关闭 HUD。CCActivityHUD
也支持这一点。
// Set success to NO will show a cross, and vice versa, YES means showing a tick.
// If you set the text to nil, self.activityHUD will only show a tick or cross according to the success parameter.
[self.activityHUD dismissWithText:@"This is a sample dismiss text" delay:0.7 success:NO]
// Set public properties before showing it.
// Set the backgrond color. The default color is black.
self.activityHUD.backColor = <#UIColor#>;
// Set the background border color. The default background color is black.
self.activityHUD.borderColor = <#UIColor#>;
// Set the backgrond border width. THe default value is 0.
self.activityHUD.borderWidth = <#CGFloat#>;
// Set the background corner radius. The default value is 5.0;
self.activityHUD.cornerRadius = <#CGFloat#>;
// Set the indicator color. The default color is white.
self.activityHUD.indicatorColor = <#UIColor#>;
// Set the boolean value that indicates whether ohter UIViews are user-interactable. The default value is YES.
self.activityHUD.isTheOnlyActiveView = <#BOOL#>;
// Set the appear animation type.
self.activityHUD.appearAnimationType = <#CCActivityHUDAppearAnimationType#>;
// Set the disappear animation type.
self.activityHUD.disappearAnimationType = <#CCActivityHUDAppearAnimationType#>;
// Set the overlay type
self.activityHUD.overlayType = <#CCActivityHUDOverlayType#>;
typedef NS_ENUM(NSInteger, CCActivityHUDIndicatorType) {
CCActivityHUDIndicatorTypeScalingDots, // Default type
CCActivityHUDIndicatorTypeLeadingDots,
CCActivityHUDIndicatorTypeMinorArc,
CCActivityHUDIndicatorTypeDynamicArc,
CCActivityHUDIndicatorTypeArcInCircle,
CCActivityHUDIndicatorTypeSpringBall,
CCActivityHUDIndicatorTypeScalingBars,
CCActivityHUDIndicatorTypeTriangleCircle
};
typedef NS_ENUM(NSInteger, CCActivityHUDAppearAnimationType) {
CCActivityHUDAppearAnimationTypeSlideFromTop,
CCActivityHUDAppearAnimationTypeSlideFromBottom,
CCActivityHUDAppearAnimationTypeSlideFromLeft,
CCActivityHUDAppearAnimationTypeSlideFromRight,
CCActivityHUDAppearAnimationTypeZoomIn,
CCActivityHUDAppearAnimationTypeFadeIn // Default type
};
typedef NS_ENUM(NSInteger, CCActivityHUDDisappearAnimationType) {
CCActivityHUDDisappearAnimationTypeSlideToTop,
CCActivityHUDDisappearAnimationTypeSlideToBottom,
CCActivityHUDDisappearAnimationTypeSlideToLeft,
CCActivityHUDDisappearAnimationTypeSlideToRight,
CCActivityHUDDisappearAnimationTypeZoomOut,
CCActivityHUDDisappearAnimationTypeFadeOut // Default type
};
typedef NS_ENUM(NSInteger, CCActivityHUDOverlayType) {
CCActivityHUDOverlayTypeNone, // Default type
CCActivityHUDOverlayTypeBlur,
CCActivityHUDOverlayTypeTransparent,
CCActivityHUDOverlayTypeShadow
};
从 v.2.1.1 开始,您可以自定义指示器形状和指示器动画。
[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
用于绘制指示器形状。您可以使用它与UIBezierPath
绘制更复杂的形状。有关CAShapeLayer
和UIBezierPath
的更多详细信息,您可以访问 CAShapeLayer 类参考和UIBezierPath 类参考。replicatorLayer
可以创建指定数量的shapeLayer
的副本。它是shapeLayer
的父图层。有关CAReplicatorLayer
的更多详细信息,您可以访问CAReplicatorLayer 类参考。animationGroup
是要应用于shapeLayer
的动画。有关CAAnimationGroup
的更多详细信息,您可以访问CAAnimationGroup 类参考。注意:您不应该在viewDidLoad
中调用showWithShape: AnimationGroup:
,动画将不会工作!相反,在viewWillAppear
或viewDidAppear
中显示。
uiimage-from-animated-gif:一个加载动画 GIF 的 UIImage 分类。它为我提供了一些精良的 API,以轻松地将 GIF 集成到CCActivityHUD
中。
欢迎提交拉取请求和问题。