为NSTimer添加暂停功能 [英] Adding pause functionality for NSTimer

查看:98
本文介绍了为NSTimer添加暂停功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是Xcode中秒表的一些工作代码。我有两个按钮,开始停止。按下按钮时,它们的标题会发生变化。现在我想添加一个暂停功能。我知道有很多线索,但(我不知道为什么)我无法使它工作。

First, here is some working code for a stopwatch in Xcode. I got two buttons, Startand Stop. Their titles change when the buttons are pressed. Now I want to add a pause functionality. I know that there are many threads about this, but (I don't know why) I was not able to get it working.

那么什么是最好的方法在我的代码中实现此功能

So what is the best approach to implement this function in my code?

我已经尝试使用暂停日期并从我的 NSTimeInterval <中删除它/ code>但得到负值......

I already tried to use a pause date and subtract it from my NSTimeIntervalbut got negative values ...

到目前为止,谢谢!

所以我这样做了:

//use timer to update the ui only, store start date (NSDate) and time interval elapsed (NSTimeInterval) 

//this is called when the timer is not running, nor paused - a sort of 'first run' function
-(void)onTimerStart
{
//zero the time elapsed
time_passed = 0;

//save the starting date
start_date = [NSDate date];

//start a timer that will update the ui in the onTimer function
timer = [NSTimer timerWithTimeInterval:1.0/10.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

//called when the timer is running to pause it
-(void)onPause
{
//calculate the time that passed ( += not = )
time_passed += [[NSDate date] timeIntervalSinceDate: start_date];

//stop the timer
[timer invalidate];

//you can get rid of the start date now (using ARC ........)
}

//restarting the timer that was paused
-(void)onUnpause
{
//get new start date
start_date = [NSDate date];

//start the timer to update ui again
timer = [NSTimer timerWithTimeInterval:1.0/10.0 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}

//use this function if you are stopping - not pausing! - the timer.
-(void)onReset
{
//stop timer
[timer invalidate];

//calculate the final time that passed
//THE NEXT LINE IS PROBABLY WRONG AND HAS TO BE time_passed = 0; THEN IT WORKS
time_passed += [[NSDate date] timeIntervalSinceDate: start_date];

//get rid of the start date now
}

//use this function to update UI - this is what gets called by the timer
-(void)onTimer
{
//when timer ticks use ([[NSDate date] timeIntervalSinceDate: start_date] + time_passed)
//to get the amount of time passed for display
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self onTimerStart];
}

工作代码:

#pragma mark - Timer

- (void)timer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
timerLabel.text = timeString;
}

#pragma mark - Stopwatch

- (IBAction)onStartPressed:(UIButton *)sender
{
if ([sender.titleLabel.text isEqualToString:@"Start"] && (![timer isValid]) && ([timerLabel.text isEqualToString:@"00:00.0"]))
{
    startDate = [NSDate date];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
                                             target:self
                                           selector:@selector(timer)
                                           userInfo:nil
                                            repeats:YES];
}
}

- (IBAction)onStopPressed:(UIButton *)sender
{
if ((![timer isValid]) && ([sender.titleLabel.text isEqualToString:@"Reset"]))
{
    [timer invalidate];
    timer = nil;
    timerLabel.text = @"00:00.0";
    [sender setTitle:@"Stop" forState:UIControlStateNormal];
}

if (([timer isValid]) && ([sender.titleLabel.text isEqualToString:@"Stop"]))
{
    [timer invalidate];
    timer = nil;
    [sender setTitle:@"Reset" forState:UIControlStateNormal];
}
}


推荐答案

那里NSTimer没有内置暂停/恢复功能,因此您必须实现以下内容:

There is no pause/resume functionality built into NSTimer, so you have to implement something along the lines of:

//THIS IS PSEUDOCODE

//use timer to update the ui only, store start date (NSDate) and time interval elapsed (NSTimeInterval) 

//this is called when the timer is not running, nor paused - a sort of 'first run' function
- onTimerStart:
{
 //zero the time elapsed
 time_passed = 0;

 //save the starting date
 start_date = [NSDate date];

 //start a timer that will update the ui in the onTimer function
 [timer start]
}

//called when the timer is running to pause it
- onPause
{
 //calculate the time that passed ( += not = )
 time_passed += [[NSDate date] timeIntervalSinceDate: start_date];

 //stop the timer
 [timer invalidate];

 //you can get rid of the start date now
}

//restarting the timer that was paused
- onUnpause
{
 //get new start date
 start_date = [NSDate];

 //start the timer to update ui again
 [timer start];
}

//use this function if you are stopping - not pausing! - the timer.
- onReset
{
 //stop timer
 [timer invalidate];

 //calculate the final time that passed
 time_passed += [[NSDate date] timeIntervalSinceDate: start_date];

 //get rid of the start date now
}

//use this function to update UI - this is what gets called by the timer
- onTimer
{
 //when timer ticks use ([[NSDate date] timeIntervalSinceDate: start_date] + time_passed)
 //to get the amount of time passed for display
}

这篇关于为NSTimer添加暂停功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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