IOS NSTimer在主线程外继续执行

来源:互联网 发布:魔兽争霸同步数据失败 编辑:程序博客网 时间:2024/05/17 07:21

转至:http://blog.csdn.net/lengshengren/article/details/12905635。

我们通常在主线程中使用NSTimer,有个实际遇到的问题需要注意。当滑动界面时,系统为了更好地处理UI事件和滚动显示,主线程runloop会暂时停止处理一些其它事件,这时主线程中运行的 NSTimer就会被暂停。解决办法就是改变NSTimer运行的modemode可以看成事件类型),不使用缺省的 NSDefaultRunLoopMode,而是改用NSRunLoopCommonModes,这样主线程就会继续处理NSTimer事件了。具体代码如下:

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timer:) userInfo:nil repeats:YES];

[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];


0 0