ios - 主线程上的 Swift UIProgressView,更新属性但不更新 UI

标签 ios multithreading swift

我有一个 UIProgressView 设置为 IBOutlet,我正在尝试调用 setProgress 方法。第一次在 viewDidLayoutSubviews() 中调用时它工作得很好,但在其他任何地方都没有。

我在主线程上使用

调用它
func calculateEXP()
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {

        //Determining playerExpBar's new value  
        let gainedEXP = Float.random(lowerEXP...upperEXP)
        let progress = (currentEXP + gainedEXP) / maxEXP

        //Setting playerExpBar's progress on the main thread
        dispatch_async(dispatch_get_main_queue()) {
             //Set value to 0.5 for testing purposes
             self.playerExpBar.setProgress(0.5, animated: true)

             print(self.playerExpBar.progress)
        }
    }

它正在打印更新后的值,但不会反射(reflect)在 UI 中。

在 viewDidLayoutSubviews() 中调用时工作

override func viewDidLayoutSubviews() {
    dispatch_async(dispatch_get_main_queue()) {
        self.playerExpBar.setProgress(0.0, animated: true)
    }
}

最佳答案

就像你说的,“在主线程上 Swift UIProgressView,更新属性但不更新 UI”,你应该检查你的 UI 属性指示器,设置一个具有 UIProgressView 对象宽度约束的值。

关于ios - 主线程上的 Swift UIProgressView,更新属性但不更新 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32547301/

相关文章:

swift - 如何将 int 转换为 tableview 单元格中的索引路径?

swift - iOS Xcode Storyboard : How do I make text look like this design

ios - 多个UIWindows状态栏冲突

php - 将 iOS 设备 token 存储到 MySQL 数据库

objective-c - iOS/Objective C- 来自 NSMutableURLRequest 的空响应

c# - 异步/等待与 Task.Run 在 C# 中

C# Winforms : A thread that can control the UI and can "sleep" without pausing the whole program. 怎么办?

ios - 将 UITextField 输入限制为 Swift 中的数字

ios - 使用 GCD 时 UITableViewCell 数据被混淆

Java执行器: How to notify a single waiting thread within the executer?