starwar2012 2013-01-08 02:28 采纳率: 0%
浏览 3797
已采纳

NSTimer没有执行预期效果

不知道我的问题是不是很好解决,请大家帮忙

有一个类,是NSTimer类型的timer,当执行IBAction时,调用一个方法,里面有下面代码:

if (timer) timer = nil;
timer = [NSTimer timerWithTimeInterval:0.2 target:self selector:@selector(removeOverlay) userInfo:nil repeats:NO];

类中还有另一个方法:

- (void)removeOverlay {
...
}

但是没有像预期的间隔0.2秒之后触发

不知道为什么?

  • 写回答

2条回答

  • ReyZhang 移动开发领域新星创作者 2013-01-08 03:46
    关注

    不用scheduled方式初始化的,需要手动addTimer:forMode: 将timer添加到一个runloop中。而scheduled的初始化方法将以默认mode直接添加到当前的runloop中.

    举例:

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerFired:) userInfo:nil repeats:NO];
    

    NSTimer *myTimer = [NSTimer timerWithTimeInterval:3.0 target:selfselector:@selector(timerFired:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:myTimer forMode:NSDefaultRunLoopMode];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)