我的应用程序中有一个下载过程,我想在下载开始直到它完成时开始更改下载按钮颜色。是否有任何动画可以更改按钮标题颜色的起点和终点?我不知道动画的持续时间,因为它取决于互联网连接和文件大小。我有以下动画,但它与我想要的不一样:
[UIView animateWithDuration:1.0 animations:^{
dlButton.backgroundColor = [UIColor MyColor];
}];
答案 0 :(得分:2)
我认为您需要在需要更新进度时手动更改backgroundColor
。
首先,获取下载进度:
CGFloat progress = xxxx
然后你需要实现一个方法,按照这样的进度更新backgroundColor
:
- (void)changeColorByProgress:(CGFloat)progress {
CGFloat red, green, blue;
[[UIColor grayColor] getRed:&red green:&green blue:&blue alpha:NULL];
CGFloat completedRed, completedGreen, completedBlue;
[[UIColor whiteColor] getRed:&completedRed green:&completedGreen blue:&completedBlue alpha:NULL];
[self.button setTitleColor:[UIColor colorWithRed:red + (completedRed - red) * progress
green:green + (completedGreen - green) * progress
blue:blue + (completedBlue - blue) * progress
alpha:1]
forState:UIControlStateNormal];
}
检查我的示例项目: https://www.dropbox.com/s/nxq90sz3w65oa0k/TestManualChangeColor.zip?dl=0
答案 1 :(得分:1)
如果您使用AFNetworking下载文件,则可以轻松跟踪其进度。在块中调用delagate方法以更新进度条的背景颜色。
检查以下链接:http://cocoadocs.org/docsets/AFNetworking/2.0.1/Categories/UIProgressView+AFNetworking.html
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){
NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES) firstObject]];
return [documentsDirectoryPath URLByAppendingPathComponent:[targetPath lastPathComponent]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error){
NSLog(@"File downloaded to: %@", filePath);
}];
[self.progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
[downloadTask resume];
您还可以通过调用自定义方法并在该方法中更改进度条颜色来更改颜色。
答案 2 :(得分:-1)
[yourButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];