GitHub 地址:https://github.com/jdg/MBProgressHUD
这个第三方框架是写加载时的一些加载效果的代码框架,使用起来也比较简单。
下载下来后直接把MBProgressHUD.h和MBProgressHUD.m拖入工程中就行,需要选择拷贝到工程,不然程序发到别的电脑上你会发现,好多错误提示。 创建一个写实现方法的 ViewController (你需要加上该方法的 vc) ,还有一个改变加载动画提示框的颜色的协议即可。
在实现方法的界面,首先要引用第三方的头文件,即:MBProgressHUD.h
其次,需要声明一个类方法:
@implementation MBExample
+ (instancetype)exampleWithTitle:(NSString *)title selector:(SEL)selector {
MBExample *example = [[self class] new];
example.title = title;
example.selector = selector;
return example;
}
@end
接下来需要你在需要加载动画的地方,添加声明方法了,在这里,demo中提供了几个加载效果的演示,分成四个区展示给大家。
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)]]
];
然后就是在上述中的选择器里写上实现方法了,例如:
- (void)indeterminateExample {
// 将 HUD 放在根视图上. (当前试图是 scroll view 可滚动,因此不适和, HUD 应该随着我们的内容滚动)
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];
});
});
}
别忘了写上每个你需要的加载动画的一些附加的方法的实现功能,比如你想加个取消加载:
- (void)cancelWork:(id)sender {
self.canceled = YES;
}
再比如,你加载动画的存在时长:
- (void)doSomeWork {
// 需等待的时长
sleep(5.);
}
等等。。 详细方法请看demo
到此,应该完成的差不多了,然后,我们需要实现协议方法:
- (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;
});
}