如何暂停/播放NSTimer? [英] How to Pause/Play NSTimer?
本文介绍了如何暂停/播放NSTimer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要以0:0:0的方式启动NSTimer我正在做的事情,我点击了一个暂停按钮,如果用户点击播放,定时器必须暂停一段时间,它必须从暂停的时间值开始运行。如何暂停和玩NSTimer?
有什么帮助吗?在此先感谢。
I need to start NSTimer by 0:0:0 that I am doing and I have a Pause button on click of that the timer has to pause after some time if a user clicks play it has to run from paused value of time. How can I pause and play in NSTimer? Any help? Thanks in advance.
推荐答案
试试这个shizzle ...对我来说很棒
Try this shizzle... worked great for me
编辑
说实话我的实际代码是:
To be honest my actual code is this:
-(IBAction)clicked:(id)sender
{
if ([startStop.titleLabel.text isEqualToString:@"Start"])
{
[startStop setTitle:@"Pause" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES];
timerDown = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
} else if ([startStop.titleLabel.text isEqualToString:@"Pause"])
{
[startStop setTitle:@"Resume" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor colorWithRed:0/255 green:0/255 blue:255/255 alpha:1.0] forState:UIControlStateNormal];
pauseStart = [[NSDate dateWithTimeIntervalSinceNow:0] retain];
previousFireDate = [[timer fireDate] retain];
[timer setFireDate:[NSDate distantFuture]];
} else if ([startStop.titleLabel.text isEqualToString:@"Resume"])
{
[startStop setTitle:@"Pause" forState:UIControlStateNormal];
[startStop setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
float pauseTime = -1*[pauseStart timeIntervalSinceNow];
[timer setFireDate:[NSDate dateWithTimeInterval:pauseTime sinceDate:previousFireDate]];
[pauseStart release];
[previousFireDate release];
}
}
我使用了UIButton并在点击时使用了这段代码。
I used a UIButton and used this code when clicked.
记得放这个:
NSDate *pauseStart, *previousFireDate;
编辑!
这里是countUp方法。忘记了down方法。秒,分钟,小时是Ints
here is the countUp method. forget the down method. seconds, minutes, hours are Ints
- (void)countUp
{
seconds += 1;
if (seconds == 60)
{
seconds = 0;
minutes++;
if (minutes == 60)
{
minutes = 0;
hours++;
}
}
}
这篇关于如何暂停/播放NSTimer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文