我使用 NSTimer,它每秒触发一次并更新一个标签,该标签显示事件发生之前的剩余时间。
到目前为止我工作得很好。问题是当我滚动 TableView 时,我的标签没有更新,因为主线程被触摸/滚动事件阻止。
我考虑过为计时器创建第二个线程,但无论如何我都无法从后台线程更新标签。我必须在 MainThread 上使用 PerformSelector... 对它进行排队,它会像以前一样卡住。
有什么方法可以在滚动时更新标签吗?
最佳答案
问题是当主线程跟踪触摸时,不会调用 ScheduledTimer。您需要在主运行循环中安排计时器。
所以而不是这样做
[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];
关于iphone - 如何在 iOS 上定期(每秒)更新标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6229759/