一、下载转圈的开源代码
地址:转圈地址跳转。
二、在list中的cell中添加该view
<UAProgressView id="progressView"class="progressView"/>
三、在cell的load函数中初始化
self.progressView.borderWidth = 2;
self.progressView.lineWidth = 4;
self.progressView.tintColor = [UIColor colorWithRed:0.20f green:0.78f blue:0.49f alpha:1.00f];
$(@"#progressView").HIDE();
四、在点击下载时,进行显示、并根据下载量,设置进度
BeeUIScrollView item = ((BeeUIScrollItem *)self.list.items[index].view;
//url表示要请求的网络资源
NSURL* url = [[NSURL alloc] initWithString:info.Url];
ASIHTTPRequest* request = [ASIHTTPRequest requestWithURL:url];
request.delegate = self;
$(item).FIND(@"progressView").SHOW();
request.showAccurateProgress = YES;
request.downloadDestinationPath = filePath;
request.downloadProgressDelegate = self;//这块这只为self,否则,在当前文件取不到,当前的下载量
[request setDidReceiveResponseHeadersSelector:@selector(didReceiveResponseHeaders:)];
//发送网络请求
[request startAsynchronous];
[request setCompletionBlock:^{
NSLog(@"下载完成");
}];
};
- (void)didReceiveResponseHeaders:(ASIHTTPRequest *)request
{
self.dataBytes = [request.responseHeaders valueForKey:@"Content-Length"];
NSLog(@"didReceiveResponseHeaders %@",[request.responseHeaders valueForKey:@"Content-Length"]);
}
六、获取当前已下载的字节数,并设置进度
- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
_receiveBytes += bytes;
UAProgressView* view = (UAProgressView*)$(self.item).FIND(@"progressView").view; //上文中的item要设置为全局的,这块使用
float progress = (float)_receiveBytes/[self.dataBytes intValue];
_localProgress = _localProgress + bytes % [self.dataBytes intValue];
[view setProgress:progress];
}
注:参考的网址阅读
iOS网络请求框架ASIHTTPRequest的一些tips