1. MBProgressHUD
項(xiàng)目中經(jīng)常會(huì)用到MBProgressHUD校辩,升級(jí)到1.0版本后填抬,很多屬性發(fā)生了變化,demo提供的示例也幾乎全用GCD的寫法烛芬,代碼顯得更加簡(jiǎn)明緊湊。認(rèn)真看了一下飒责,收獲頗豐赘娄。
#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; //背景框透明度,默認(rèn)為0.8
hud.bezelView.color = [UIColor grayColor]; //背景框顏色
hud.bezelView.layer.cornerRadius = 10;//背景框圓角宏蛉,默認(rèn)是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;//提示框相對(duì)于父視圖的偏移量
hud.offset = CGPointMake(0, 20);
hud.minSize = CGSizeMake(50, 50);//設(shè)置背景框最小尺寸遣臼,實(shí)際大小為readonly
hud.margin = 5;//設(shè)置各元素距離矩形邊框的距離
hud.square = NO;//強(qiáng)制背景框?yàn)檎叫? hud.removeFromSuperViewOnHide = YES;//隱藏時(shí)從父視圖刪除
//1.0后最大變化也許就是新增button屬性
[hud.button setTitle:@"cancel" forState:UIControlStateNormal];
[hud.button addTarget:self action:@selector(go) forControlEvents:UIControlEventTouchUpInside];
//進(jìn)度條
hud.mode = MBProgressHUDModeAnnularDeterminate;
// HUD.mode = MBProgressHUDModeIndeterminate;//菊花,默認(rèn)值
// HUD.mode = MBProgressHUDModeDeterminate;//餅狀圖
// HUD.mode = MBProgressHUDModeDeterminateHorizontalBar;//進(jìn)度條
// HUD.mode = MBProgressHUDModeAnnularDeterminate;//圓環(huán)作為進(jìn)度條
// HUD.mode = MBProgressHUDModeCustomView; //需要設(shè)置自定義視圖時(shí)候設(shè)置成這個(gè)
// 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 是一個(gè)專做做環(huán)形進(jìn)度條的第三方框架,使用起來(lái)也是十分的方便嗅义。
//懶加載
- (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;
=====對(duì)界面進(jìn)行刷新全部使用主線程========
dispatch_async(dispatch_get_main_queue(), ^{
self.progressView.progress = progress;
});
usleep(50000);//微秒屏歹,千分之一毫秒
}
}
DACircularProgress 還有一個(gè)子類,在加載圈中間顯示進(jìn)度
- (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 是另一款易用的進(jìn)度框架之碗,由于其單例 設(shè)計(jì)蝙眶,不用每次初始化,大多方法是加號(hào)方法褪那。進(jìn)度條都需要注意的是刷新UI一定要在主線程中進(jìn)行幽纷,并且示例代碼中也幾乎全是用GCD在處理線程。
- (void)go{
// [SVProgressHUD show];
// [SVProgressHUD showWithStatus:@"我愛(ài)你"];
// [SVProgressHUD showSuccessWithStatus:@"我愛(ài)你"];
// [SVProgressHUD showInfoWithStatus:@"我愛(ài)你嗎"];
// [SVProgressHUD showErrorWithStatus:@"我不愛(ài)你"];
[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
除了簡(jiǎn)單的效果博敬,JHUD 可以使用圖片數(shù)組友浸,或gif 圖快速的自定義ProrgerssView
//源碼中建議寫成懶加載
self.hudView = [[JHUD alloc]initWithFrame:self.view.bounds];
//self.hudView.indicatorBackGroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.1];
// self.hudView.indicatorForegroundColor = [UIColor redColor];
//類型為JHUDLoadingTypeFailure時(shí),設(shè)置的回調(diào)方法
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:@"我愛(ài)你" hudType:JHUDLoadingTypeCircle];
[JHUD hideForView:self.view];