ios – 进度视图未更新 – Swift

我正在使用 Swift中的ProgressView

这是我的代码

var request = HTTPTask()
            let downloadTask = request.download(urlm!,parameters: nil,progress: {(complete: Double) in
                println("percent complete: \(Float(complete))")
                self.Progress.setProgress(Float(complete),animated: true)

即使完整提供此输出

percent complete: 0.00480848
percent complete: 0.0089619
percent complete: 0.0132512
percent complete: 0.0175405
percent complete: 0.0221036
percent complete: 0.0264841
percent complete: 0.0308647
percent complete: 0.0350627
percent complete: 0.0392608
percent complete: 0.0434588
percent complete: 0.0513073
percent complete: 0.0555054
percent complete: 0.0598859
percent complete: 0.0642665
percent complete: 0.0688296
percent complete: 0.0731189
percent complete: 0.0774994
percent complete: 0.0817887
percent complete: 0.0861693
percent complete: 0.0908237
percent complete: 0.095478
percent complete: 0.099676
percent complete: 0.104057
percent complete: 0.109624
percent complete: 0.114278
percent complete: 0.119024
percent complete: 0.123404
percent complete: 0.127602
percent complete: 0.131983
percent complete: 0.136181
percent complete: 0.136408
percent complete: 0.140561
percent complete: 0.144759
percent complete: 0.148957
percent complete: 0.153155
percent complete: 0.157901
percent complete: 0.162282
percent complete: 0.166662
percent complete: 0.171043
percent complete: 0.175241
percent complete: 0.175468
percent complete: 0.179621
percent complete: 0.184002
percent complete: 0.188291
percent complete: 0.192672
percent complete: 0.197052
percent complete: 0.201524
percent complete: 0.205905
percent complete: 0.210285
percent complete: 0.21494
percent complete: 0.215258
...
percent complete: 1.0

它不会在此更新进度视图

self.Progress.setProgress(Float(complete),animated: true)

解决方法

针对Swift 3进行了更新

对UI的更改应始终发生在主/前台线程上,在这种情况下,它发生在后台队列上.使用:

DispatchQueue.main.async {
    self.Progress.setProgress(Float(complete),animated: true)
}

把它移到前台.

前Swift 3的旧答案

对UI的更改应始终发生在主/前台线程上,它发生在后台队列上.使用:

dispatch_async(dispatch_get_main_queue()) {
    self.Progress.setProgress(Float(complete),animated: true)
}

把它移到前台.

相关文章

背景 前端时间产品经理决定使用百度统计,使得 工程B 中原统计sdk-友盟统计,需要被去除。之前尝试去除...
结论: alloc负责分配内存和创建对象对应的isa指针; init只是返回alloc生成的对象。 所以alloc后,多次...
更新 如果UI愿意把启动图切割成n份,按一定约束在launchscreen.storyboard中进行排版,启动图效果会更好...
最近在看一本书《Effective OC 2.0》,今天看到有个tip是OC适中循环各自优劣性,作者最终推荐此块循环。...
// // ViewController.m // paintCodeTestOC //gif // Created by LongMa on 2019/7/25. // #import &a...
背景介绍 一般情况下,出于省电、权限、合理性等因素考虑,给人的感觉是很多奇怪的需求安卓可以实现,但...