如何在 iOS 上定期更新标签(每秒)?

How update a label periodically on iOS (every second)?(如何在 iOS 上定期更新标签(每秒)?)
本文介绍了如何在 iOS 上定期更新标签(每秒)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用每秒触发一次的 NSTimer 并更新一个显示事件前剩余时间的标签.

I use a NSTimer which fires every second and updates a label which displays the remaining time until an event.

到目前为止我工作得很好.问题是当我滚动 TableView 时,我的标签没有更新,因为 MainThread 被触摸/滚动事件阻塞.

I works fine so far. The problem is while I am scrolling the TableView my Label does not update, because the MainThread is blocked by the touch/scroll event.

我曾考虑为 Timer 创建第二个线程,但无论如何我无法从后台线程更新标签.我不得不将它与 performSelector... 放在 MainThread 上,它会像以前一样卡住.

I thought about creating a second thread for the Timer but I couldn't update the label from a background thread anyways. I had to queue it with performSelector... on the MainThread where it would stuck like before.

有没有办法在滚动时更新标签?

Is there any way to update the label while scrolling?

推荐答案

问题是在主线程跟踪触摸时,scheduledTimer 不会被调用.您需要在主运行循环中安排计时器.

The problem is that a scheduledTimer will not get called while the main thread is tracking touches. You need to schedule the timer in the main run loop.

所以不要这样做

[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];

使用

NSTimer* timer = [NSTimer timerWithTimeInterval:1.0f target:self selector:@selector(updateLabel:) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

这篇关于如何在 iOS 上定期更新标签(每秒)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)