拖动UITableView时NSTimer无法正常工作 [英] NSTimer not working while dragging a UITableView

查看:89
本文介绍了拖动UITableView时NSTimer无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有倒数计时器的应用程序.我已经用一个标签进行了更新,该标签通过以下方式从计时器调用的函数进行了更新:

I have an application with a countdown timer. I've made it with a label that is updated with a function called from a timer this way:

...
int timeCount = 300; // Time in seconds
...
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(actualizarTiempo:) userInfo:nil repeats:YES];
...
- (void)actualizaTiempo:(NSTimer *)timer {
    timeCount -= 1;
    if (timeCount <= 0) {
        [timer invalidate];
    } else {
        [labelTime setText:[self formatTime:timeCount]];
    }
}

注意:formatTime是一个接收整数(秒数)并返回格式为mm:ss的NSString的函数

Note: formatTime is a function which receive an integer (number of seconds) and returns a NSString with format mm:ss

一切正常,也就是说,时间倒计时了,但问题是我在应用程序中有一个UITableView,如果我触摸表格并将其拖动(沿单元格移动),计时器将停止直到我松开手指从屏幕上...

Everything works ok, that is, the time counts down but the problem is that I have a UITableView in the application and if I touch the table and drag it (to move along the cells) the timer stops until I release my finger from the screen...

这种行为正常吗?如果是,是否有任何方法可以避免这种情况,并在拖动表时使计时器工作?

Is this behavior normal? If it is, is there any way to avoid it and make the timer work while dragging the table?

推荐答案

如j.tom.schroeder所述,通过使用scheduledTimerWithTimeInterval:,您的计时器会自动在默认模式下的主运行循环中进行调度.当运行循环处于非默认模式(例如,点击或滑动)时,这将防止计时器触发.

By using scheduledTimerWithTimeInterval:, as j.tom.schroeder says, your timer is automatically schedule on the main run loop for the default modes. This will prevent your timer from firing when your run loop is in a non-default mode, e.g., when tapping or swiping.

但是,解决方案不是使用线程,而是为所有常见模式安排计时器:

The solution, though, is not using a thread, but scheduling your timer for all common modes:

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

根据您希望在不停止计时器的情况下允许的事件类型,您也可以考虑UITrackingRunLoopMode.有关运行循环模式的更多信息,请参见

Depending on the kind of events you would like to allow without them stopping your timers, you might also consider UITrackingRunLoopMode. For more info about run loop modes, see Apple Docs.

这篇关于拖动UITableView时NSTimer无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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