在后台线程中使用计时器更新UI

编程入门 行业动态 更新时间:2024-10-11 15:21:18
本文介绍了在后台线程中使用计时器更新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

本文发布于:2023-11-27 16:29:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638750.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计时器   线程   后台   UI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!