在后台线程中使用计时器更新UI [英] Using a Timer in the background thread to update UI

查看:79
本文介绍了在后台线程中使用计时器更新UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我有一个计时器,最长可长达5分钟.用户可以将元素添加到数组中,从而将计时器重置为5分钟.当计时器计数城镇时,还有一个UIView会将其宽度按计时器的比例从视图的100%更改为视图的0%.

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.

我想在后台线程上运行计时器,以避免由于其他UI事件发生而导致计时器启动延迟.

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.

是否可以在后台线程中运行一个计时器,该计时器调用的关联函数可以更新UI中的标签或视图宽度,而不会导致线程问题/崩溃?到目前为止,我还无法正常工作.

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.

推荐答案

只需从主线程更新UI.

Just need to update your UI from the main thread.

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

这篇关于在后台线程中使用计时器更新UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆