gpt4 book ai didi

ios - 自定义动画不起作用

转载 作者:行者123 更新时间:2023-11-29 11:57:17 24 4
gpt4 key购买 nike

我想下载带有自定义动画的文件。但是当我使用第一个代码时动画不起作用

第一个代码

ViewController.h

@property (nonatomic, strong) PWMainView *mainView; // custom animation view

ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

self.mainView = [[PWMainView alloc] init];
[self.mainView.progressView setProgress:0];
[self.view addSubview: self.mainView];

_session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
[self.progressView setProgress:0 animated:NO];
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite;{

[self.progressView setProgress:totalBytesWritten/totalBytesExpectedToWrite animated:YES];

}
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Warning.png"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:NO];


NSData *urlData = [NSData dataWithContentsOfURL:_url];
[urlData writeToFile:filePath atomically:YES];

float progress = [urlData length]/(float)[urlData length];
[self.mainView.progressView setProgress:progress];

if (!fileExists) {
NSLog(@"!fileExists");
}

if (fileExists) {
NSLog(@"fileExists");
}


}

-(IBAction) downloadButton:(id)sender
{
if (_HighScore == 1) {
if(_downloadTask == nil){


_url =[NSURL URLWithString:@"http://www.freeiconspng.com/uploads/ios-png-6.png"];

_downloadTask = [_session downloadTaskWithURL:_url];

[_downloadTask resume];

}

else

[_downloadTask resume];
}
[_downloadView removeFromSuperview];
[_downloadButton removeFromSuperview];
[_downloadButtonCancel removeFromSuperview];

}

如果我使用第二个代码动画工作。但是我需要使用第一个代码。

第二个代码

ViewController.h

@property (nonatomic, strong) PWMainView *mainView; // custom animation view

ViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

self.mainView = [[PWMainView alloc] init];
[self.mainView.progressView setProgress:0];
[self.view addSubview: self.mainView];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"Chapter1.mp3"];
dispatch_async(dispatch_get_main_queue(), ^{

NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B_mHRsv5WQu6d3plcTlHV2VmaGs";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
float progress = [urlData length]/(float)[urlData length];
[self.mainView.progressView setProgress:progress];

});
}

为什么动画在第一个代码中不起作用?

更新

更改代码行。不起作用。

    dispatch_async(dispatch_get_main_queue()
{
[self.mainView.progressView setProgress:progress];
}

但是如果我更改第一个代码中的链接

NSString *stringURL = @"http://www.freeiconspng.com/uploads/ios-png-6.png";

NSString *stringURL = @"https://drive.google.com/uc?export=download&id=0B_mHRsv5WQu6d3plcTlHV2VmaGs";

一切正常。但动画不会在整个下载过程中执行。仅在最后。

最佳答案

NSURLSession 的委托(delegate)方法在后台线程上调用。您必须从主线程执行 UIKit 调用。

您需要将在这些委托(delegate)方法中执行的任何 UI 调用包装在对 dispatch_async(dispatch_get_main_queue()) 的调用中。 (好吧,有多种方法可以在 man 线程上调用 UI 代码,但这是一个很好的方法。)

改变这一行:

[self.mainView.progressView setProgress:progress];

dispatch_async(dispatch_get_main_queue()
{
[self.mainView.progressView setProgress:progress];
}

关于ios - 自定义动画不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482673/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com