GitHub? 地址:https://github.com/jdg/MBProgressHUD
這個(gè)第三方框架是寫加載時(shí)的一些加載效果的代碼框架审洞,使用起來也比較簡(jiǎn)單鸳兽。?
下載下來后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,需要選擇拷貝到工程品擎,不然程序發(fā)到別的電腦上你會(huì)發(fā)現(xiàn)埋合,好多錯(cuò)誤提示。 ?創(chuàng)建一個(gè)寫實(shí)現(xiàn)方法的 ViewController (你需要加上該方法的 vc) 萄传,還有一個(gè)改變加載動(dòng)畫提示框的顏色的協(xié)議即可甚颂。
在實(shí)現(xiàn)方法的界面,首先要引用第三方的頭文件秀菱,即:MBProgressHUD.h
其次振诬,需要聲明一個(gè)類方法:
@implementation MBExample
+ (instancetype)exampleWithTitle:(NSString *)title selector:(SEL)selector {
MBExample *example = [[self class] new];
example.title = title;
example.selector = selector;
return example;
}
@end
接下來需要你在需要加載動(dòng)畫的地方,添加聲明方法了衍菱,在這里赶么,demo中提供了幾個(gè)加載效果的演示,分成四個(gè)區(qū)展示給大家脊串。
self.examples =
@[@[[MBExample exampleWithTitle:@"Indeterminate mode" selector:@selector(indeterminateExample)],
[MBExample exampleWithTitle:@"With label" selector:@selector(labelExample)],
[MBExample exampleWithTitle:@"With details label" selector:@selector(detailsLabelExample)]],
@[[MBExample exampleWithTitle:@"Determinate mode" selector:@selector(determinateExample)],
[MBExample exampleWithTitle:@"Annular determinate mode" selector:@selector(annularDeterminateExample)],
[MBExample exampleWithTitle:@"Bar determinate mode" selector:@selector(barDeterminateExample)]],
@[[MBExample exampleWithTitle:@"Text only" selector:@selector(textExample)],
[MBExample exampleWithTitle:@"Custom view" selector:@selector(customViewExample)],
[MBExample exampleWithTitle:@"With action button" selector:@selector(cancelationExample)],
[MBExample exampleWithTitle:@"Mode switching" selector:@selector(modeSwitchingExample)]],
@[[MBExample exampleWithTitle:@"On window" selector:@selector(indeterminateExample)],
[MBExample exampleWithTitle:@"NSURLSession" selector:@selector(networkingExample)],
[MBExample exampleWithTitle:@"Dim background" selector:@selector(indeterminateExample)],
[MBExample exampleWithTitle:@"Colored" selector:@selector(indeterminateExample)]]
];
然后就是在上述中的選擇器里寫上實(shí)現(xiàn)方法了辫呻,例如:
- (void)indeterminateExample {
// 將 HUD 放在根視圖上. (當(dāng)前試圖是 scroll view 可滾動(dòng)清钥,因此不適和, HUD 應(yīng)該隨著我們的內(nèi)容滾動(dòng))
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
// 異步加載
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
[self doSomeWork];
// 回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
[hud hideAnimated:YES];
});
});
}
別忘了寫上每個(gè)你需要的加載動(dòng)畫的一些附加的方法的實(shí)現(xiàn)功能,比如你想加個(gè)取消加載:
- (void)cancelWork:(id)sender {
self.canceled = YES;
}
再比如放闺,你加載動(dòng)畫的存在時(shí)長(zhǎng):
- (void)doSomeWork {
// 需等待的時(shí)長(zhǎng)
sleep(5.);
}
等等循捺。。? 詳細(xì)方法請(qǐng)看demo
到此雄人,應(yīng)該完成的差不多了从橘,然后,我們需要實(shí)現(xiàn)協(xié)議方法:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
// 回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [MBProgressHUD HUDForView:self.navigationController.view];
UIImage *image = [[UIImage imageNamed:@"Checkmark"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
hud.customView = imageView;
hud.mode = MBProgressHUDModeCustomView;
hud.label.text = NSLocalizedString(@"Completed", @"HUD completed title");
[hud hideAnimated:YES afterDelay:3.f];
});
}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float progress = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
// 回到主線程
dispatch_async(dispatch_get_main_queue(), ^{
MBProgressHUD *hud = [MBProgressHUD HUDForView:self.navigationController.view];
hud.mode = MBProgressHUDModeDeterminate;
hud.progress = progress;
});
}