Using a Timer in the background thread to update UI

↘锁芯ラ 提交于 2019-12-24 00:53:48

问题


In my app I have a timer that can be up to 5 minutes long. The user can add an element to an array which resets the timer to 5 minutes. When the timer counts town, there's also a UIView which changes its width from 100% of the view to 0% of it in proportion to the timer.

I'd like to run my timer on a background thread to avoid a delay in the timer starting due to other UI events happening.

Is it possible to have a timer run in a background thread whose associated function that the timer calls updates a label or a view width in the UI without threading issues/crashing? So far I haven't been able to get this working.


回答1:


Just need to update your UI from the main thread.

...
dispatch_async(dispatch_get_main_queue(), { () -> Void in
   // perform view resizing
})
...


来源:https://stackoverflow.com/questions/43946214/using-a-timer-in-the-background-thread-to-update-ui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!