自定义动画不起作用

时间:2016-07-20 13:27:50

标签: ios objective-c animation

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

第一个代码

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";

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

1 个答案:

答案 0 :(得分:1)

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

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

更改此行:

[self.mainView.progressView setProgress:progress];

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