简体   繁体   中英

Swift UIProgressView on main thread, updates property but not UI

I have a UIProgressView set as an IBOutlet and I'm trying to call the setProgress method. It works just fine the first time it's called in viewDidLayoutSubviews(), but nowhere else.

I'm calling it on the main thread using

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)
        }
    }

}

It's printing the updated value but doesn't reflect in the UI.

Working when called in viewDidLayoutSubviews()

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

就像您说的“在主线程上移动UIProgressView,更新属性而不是UI”一样,您应该检查UI属性指示器,并使用UIProgressView对象的宽度约束设置一个值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM