2016-07-20 17 views
1

Ich möchte Datei mit benutzerdefinierter Animation herunterladen. Aber als ich das erste Code-Animation verwenden funktioniert nichtBenutzerdefinierte Animation funktioniert nicht

Erste Code

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

} 

Wenn ich zweite Code Animationsarbeit . Aber ich muss den ersten Code verwenden.

Zweiter Code

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

    }); 
} 

Warum Animation nicht in erster Code funktioniert?

aktualisieren

in Code Zeile ändern. Funktioniert nicht.

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

Aber wenn ich Link in Tannen Code ändern

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

auf

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

Alles funktioniert. Die Animation wird jedoch nicht während des gesamten Downloadvorgangs ausgeführt. Nur am Ende.

Antwort

1

Die Delegate-Methoden von NSURLSession werden in einem Hintergrundthread aufgerufen. Sie müssen UIKit-Aufrufe vom Hauptthread ausführen.

Sie müssen alle UI-Aufrufe umbrechen, die Sie in diesen Delegatmethoden in einem Aufruf an dispatch_async(dispatch_get_main_queue()) ausführen. (Na ja, gibt es verschiedene Möglichkeiten UI-Code auf den Mann Thread zu nennen, aber dies ist ein guter.)

Ändern Sie diese Zeile:

[self.mainView.progressView setProgress:progress]; 

Um

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

Danke für Antwort. Aktualisiere meine Frage. Wenn ich den ersten Code mit der standardmäßigen "progressView" -Animation verwende, funktioniert das. Vielleicht müssen Sie diese Linie reparieren? '[self.progressView setProgress: totalBytesWritten/totalBytesExpectedToWrite animiert: YES];' –

+0

Alle 'NSURLSession' Delegate-Methoden werden in einem Hintergrundthread aufgerufen, und alle Aufrufe, die die UI aktualisieren, müssen im Hauptthread ausgeführt werden. Daher müssen Sie ALLE Ihre Aufrufe an 'setProgress' umleiten, die aus Delegate-Methoden in Aufrufen von' dispatch_async (dispatch_get_main_queue()) 'erfolgen, wie in meiner Antwort gezeigt. –

Verwandte Themen