最近因项目需求,修改原有下载进度条,特做此记录!
为UIImage创建分类,并动态添加ProgressValue属性,根据当前进度来选择图片
#import <UIKit/UIKit.h>
@interface UIImageView (Property)
@property(nonatomic,assign)CGFloat progressValue
@end
#import "UIImageView+Property.h"
#import <objc/runtime.h>
static char progressValue;
@implementation UIImageView (Property)
- (CGFloat)progressValue
{
return [objc_getAssociatedObject(self, &progressValue) floatValue];
}
- (void)setProgressValue:(CGFloat)progressValue
{
objc_setAssociatedObject(self, &progressValue, [NSString stringWithFormat:@"%f",progressValue], OBJC_ASSOCIATION_COPY);
NSLog(@"p-%f",progressValue);
if (progressValue == 0.0) {
[self setImage:[UIImage imageNamed:@"download0"]];
}else if (0.0<progressValue <=0.1) {
[self setImage:[UIImage imageNamed:@"download1"]];
}else if (0.1<progressValue <= 0.2){
[self setImage:[UIImage imageNamed:@"download2"]];
}else if (0.2<progressValue <=0.3){
[self setImage:[UIImage imageNamed:@"download3"]];
}else if (0.3<progressValue <=0.4){
[self setImage:[UIImage imageNamed:@"download4"]];
}else if (0.4<progressValue <=0.5){
[self setImage:[UIImage imageNamed:@"download5"]];
}else if (0.5<progressValue <=0.6){
[self setImage:[UIImage imageNamed:@"download6"]];
}else if (0.7<progressValue <=0.8){
[self setImage:[UIImage imageNamed:@"download7"]];
}else if (0.8<progressValue <=0.9){
[self setImage:[UIImage imageNamed:@"download8"]];
}else if (0.9<progressValue <1.0){
[self setImage:[UIImage imageNamed:@"download9"]];
}else{
[self setImage:[UIImage imageNamed:@"download10"]];
}
}
@end
----------------------------华丽分割线---------------------
#import "ViewController.h"
#import "UIImageView+Property.h"
@interface ViewController ()
{
NSTimer *_timer;
}
@property(nonatomic,strong)UIImageView *progressView;
@end
@implementation ViewController
- (UIImageView *)progressView
{
if (!_progressView) {
_progressView = [[UIImageView alloc] init];
_progressView.center = self.view.center;
_progressView.bounds = CGRectMake(0,0,250,54);
}
return _progressView;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
[self.view addSubview:self.progressView];
NSLog(@"--------%f",self.progressView.progressValue);
if (!_timer) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(animation) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop]addTimer:_timer forMode:NSDefaultRunLoopMode];
}
}
static CGFloat value = 0;
- (void)animation
{
value += 0.1;
if (value >= 1.0) {
[_timer invalidate];
_timer = nil;
[self.progressView removeFromSuperview];
value = 0;
return;
}
self.progressView.progressValue = value;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
}
在实际项目中一般使用SDWebImageDownloader下载
[[SDWebImageDownloader sharedDownloader]downloadImageWithURL:[NSURL URLWithString:@""] options:SDWebImageDownloaderHighPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//在此给分类中的的progressVaule赋值
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
}];
运行效果图