1. MBProgressHUD
项目中经常会用到MBProgressHUD,升级到1.0版本后,很多属性发生了变化,demo提供的示例也几乎全用GCD的写法,代码显得更加简明紧凑。认真看了一下,收获颇丰。
#import "ViewController.h"
#import <MBProgressHUD.h>
@interface ViewController ()
@property (nonatomic,assign) BOOL isCancel;
@end
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
MBProgressHUD *hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.bezelView.alpha = 0.8; //背景框透明度,默认为0.8
hud.bezelView.color = [UIColor grayColor]; //背景框颜色
hud.bezelView.layer.cornerRadius = 10;//背景框圆角,默认是10;
hud.backgroundView.style = MBProgressHUDBackgroundStyleBlur;//背景模糊
//hud.activityIndicatorColor = [UIColor redColor];//菊花颜色,废弃但有效
hud.label.text = @"loading...";
hud.label.textColor = [UIColor redColor];//提示字体颜色
hud.label.font = [UIFont systemFontOfSize:13];//详情字体
hud.detailsLabel.text = @"LoadingLoading...";
hud.detailsLabel.textColor = [UIColor blueColor];//详情字体颜色
hud.detailsLabel.font = [UIFont systemFontOfSize:13];//详情字体
//hud.xOffset = 0;
//hud.yOffset = -20;//提示框相对于父视图的偏移量
hud.offset = CGPointMake(0, 20);
hud.minSize = CGSizeMake(50, 50);//设置背景框最小尺寸,实际大小为readonly
hud.margin = 5;//设置各元素距离矩形边框的距离
hud.square = NO;//强制背景框为正方形
hud.removeFromSuperViewOnHide = YES;//隐藏时从父视图删除
//1.0后最大变化也许就是新增button属性
[hud.button setTitle:@"cancel" forState:UIControlStateNormal];
[hud.button addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
//进度条
hud.mode = MBProgressHUDModeAnnularDeterminate;
// HUD.mode = MBProgressHUDModeIndeterminate;//菊花,默认值
// HUD.mode = MBProgressHUDModeDeterminate;//饼状图
// HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;//进度条
// HUD.mode = MBProgressHUDModeAnnularDeterminate;//圆环作为进度条
// HUD.mode = MBProgressHUDModeCustomView; //需要设置自定义视图时候设置成这个
// HUD.mode = MBProgressHUDModeText; //只显示文本
//1.0后几乎全是这种写法,必须保证HUD在主线程
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self dosomething];
dispatch_async(dispatch_get_main_queue(), ^{
hud.hidden = YES;
});
});
}
//
- (void)go{
self.isCancel = YES;
}
- (void)dosomething{
self.isCancel = NO;
float progress = 0.0f;
while (progress < 1.0f) {
if (self.isCancel) return;
progress += 0.01f;
dispatch_async(dispatch_get_main_queue(), ^{
[MBProgressHUD HUDForView:self.view].progress = progress;
});
usleep(50000);//微秒,千分之一毫秒
}
}
2. DACircularProgress
DACircularProgress 是一个专做做环形进度条的第三方框架,使用起来也是十分的方便。
//懒加载
- (DACircularProgressView*)progressView{
if (!_progressView) {
_progressView = [[DACircularProgressView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
_progressView.center = self.view.center;
_progressView.roundedCorners = YES;
_progressView.progressTintColor = [UIColor darkGrayColor];
_progressView.trackTintColor = [UIColor lightGrayColor];
}
return _progressView;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view addSubview:self.progressView];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self dosomething];
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.hidden = YES;
[self.progressView removeFromSuperview];
self.progressView = nil;
});
});
}
- (void)dosomething{
float progress = 0.0f;
while (progress < 1.0f) {
progress += 0.01f;
=====对界面进行刷新全部使用主线程========
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.progress = progress;
});
usleep(50000);//微秒,千分之一毫秒
}
}
DACircularProgress 还有一个子类,在加载圈中间显示进度
- (DALabeledCircularProgressView*)labeledProgressView{
if (!_labeledProgressView) {
_labeledProgressView = [[DALabeledCircularProgressView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
_labeledProgressView.roundedCorners = YES;
_labeledProgressView.center = self.view.center;
}
return _labeledProgressView;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self.view addSubview:self.labeledProgressView];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self dosomething];
dispatch_async(dispatch_get_main_queue(), ^{
self.labeledProgressView.hidden = YES;
});
});
}
3. SVProgressHUD
SVProgressHUD 是另一款易用的进度框架,由于其单例 设计,不用每次初始化,大多方法是加号方法。进度条都需要注意的是刷新UI一定要在主线程中进行,并且示例代码中也几乎全是用GCD在处理线程。
- (void)go{
// [SVProgressHUD show];
// [SVProgressHUD showWithStatus:@"我爱你"];
// [SVProgressHUD showSuccessWithStatus:@"我爱你"];
// [SVProgressHUD showInfoWithStatus:@"我爱你吗"];
// [SVProgressHUD showErrorWithStatus:@"我不爱你"];
[SVProgressHUD showProgress:0 ];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// time-consuming task
[self doSomeThing];
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD dismiss];
});
});
}
- (void)doSomeThing{
CGFloat progress = 0.0;
while (progress < 1.0) {
dispatch_async(dispatch_get_main_queue(), ^{
[SVProgressHUD showProgress:progress status:[NSString stringWithFormat:@"%.2f%%",progress]];
if (progress > 1.0) {
return ;
}
});
}
}
4. JHUD
除了简单的效果,JHUD 可以使用图片数组,或gif 图快速的自定义ProrgerssView
//源码中建议写成懒加载
self.hudView = [[JHUD alloc]initWithFrame:self.view.bounds];
//self.hudView.indicatorBackGroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.1];
// self.hudView.indicatorForegroundColor = [UIColor redColor];
//类型为JHUDLoadingTypeFailure时,设置的回调方法
typeof(self) weakself = self;
[self.hudView setJHUDReloadButtonClickedBlock:^{
[weakself go];
}];
JHUD 共5种类型
//hudType:
//JHUDLoadingTypeCircle = 0,
//JHUDLoadingTypeCircleJoin = 1,
//JHUDLoadingTypeDot = 2,
self.hudView.messageLabel.text = @"Hello ,this is a circle animation";
[self.hudView showAtView:self.view hudType:JHUDLoadingTypeCircleJoin];
//JHUDLoadingTypeCustomAnimations = 3,
NSMutableArray *arr = [NSMutableArray array];
for (int i = 0; i < 2; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
[arr addObject:image];
}
self.hudView.indicatorViewSize = CGSizeMake(60, 60);
self.hudView.customAnimationImages = arr;
self.hudView.messageLabel.text = @"Hello ,this is a circle animation";
[self.hudView showAtView:self.view hudType:JHUDLoadingTypeCustomAnimations];
//JHUDLoadingTypeGifImage = 4,
NSString *path = [[NSBundle mainBundle] pathForResource:@"loadinggif3" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
self.hudView.gifImageData = data;
self.hudView.indicatorViewSize = CGSizeMake(100, 100);
self.hudView.messageLabel.text = @"please wait....";
[self.hudView showAtView:self.view hudType:JHUDLoadingTypeGifImage];
//JHUDLoadingTypeFailure = 5,
self.hudView.indicatorViewSize = CGSizeMake(100, 100);
self.hudView.messageLabel.text = @"Can't get data, please make sure the interface is correct !";
[self.hudView.refreshButton setTitle:@"Refresh" forState:UIControlStateNormal];
self.hudView.customImage = [UIImage imageNamed:@"5.jpg"];
[self.hudView showAtView:self.view hudType:JHUDLoadingTypeFailure];
//隐藏
//[self.hudView hide];
//[self.hudView hideAfterDelay:3];
//类方法
[JHUD showAtView:self.view message:@"我爱你" hudType:JHUDLoadingTypeCircle];
[JHUD hideForView:self.view];