ios - 自定义动画不起作用

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

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

最佳答案

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/

相关文章:

Android ExpandableListView 使用动画

objective-c - UITableViewController 在网络请求完成之前执行延迟功能

ios - 带有 uiwebview swift 的 Gif

ios - 在 iOS 8 中自动调整 UITableViewCell 的大小

ios - 以奇怪的行为一次关闭多个 View Controller

javascript - 使用 jQuery 启动和停止 css3 动画

Java:如何制作GUI动画?

iOS微信SDK登录集成问题: System error. 错误码:10026

ios - 如何为PayPal自适应(MPL)付款进行服务器验证

iPhone 开发 : pointer being freed was not allocated