取消暂停这个 NSTimer?
我正在暂停 NSTimer
,如下所示:
- (IBAction)pause
{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFiringDate = [stopmusictimer fireDate];
NSDate *date = [NSDate distantFuture];
[stopmusictimer setFireDate:date];
}
我有另一个 IBAction:
- (IBAction)unpausebutton
{
// dunno code
}
但我现在不确定如何取消暂停 NSTimer
。任何人,请帮助我!谢谢
I am pausing an NSTimer
like so:
- (IBAction)pause
{
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFiringDate = [stopmusictimer fireDate];
NSDate *date = [NSDate distantFuture];
[stopmusictimer setFireDate:date];
}
I have another IBAction:
- (IBAction)unpausebutton
{
// dunno code
}
But I am not sure how to unpause the NSTimer
now. Anyone, please help me! Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSTimer 实例并不是真正设计用于动态处理的;
setFireDate:
明确记录了这样做的成本相对较高。可能类似于使旧的无效并创建一个新的。如果您正在暂停计时器,然后您的“取消暂停”始终“立即启动”,我建议在暂停时使计时器无效并释放它(仅在必要时释放),然后在取消暂停时简单地调用目标方法。
这将是一个更典型的模式。
NSTimer instances weren't really designed to be dynamically mucked with; the
setFireDate:
explicitly documents that doing so is relatively expensive. Probably similar to just invalidating the old one and creating a new one.If you are pausing the timer and then your "unpause" is always "fire right now", I'd suggest invalidating and releasing the timer on pause (release only if necessary) then simply calling the targeted method on unpause.
That would be a more typical pattern.