iphone - 为 NSTimer 添加暂停功能

标签 iphone ios xcode nstimer

首先,这里是 Xcode 中秒表的一些工作代码。我有两个按钮,StartStop。当按下按钮时,它们的标题会改变。现在我想添加一个暂停功能。我知道有很多关于此的话题,但是(我不知道为什么)我无法让它工作。

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

我已经尝试使用暂停日期并将其从我的 NSTimeInterval 中减去,但得到的是负值 ...

到目前为止谢谢!

所以我这样做了:

//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 中没有内置的暂停/恢复功能,因此您必须按照以下方式实现:

//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
}

关于iphone - 为 NSTimer 添加暂停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8922269/

相关文章:

iphone - uibutton onclick 不调用/打开网页

iphone - 多个UIButton打开相同的 View 但内容不同

ios - 从主屏幕检测密码锁定状态

ios - 在模态视图 Controller 后面更改 View Controller

iphone - 如何更改 iOS 项目以完全不使用 nib,而只使用 Objective-C 代码?

iphone - NSMutableURLRequest setHTTPBody : crash

iphone - NSURL URLWithString : raises exception

ios - 删除 UITableViewCell 选择

iphone - 如何在 cocos2d 中停止预定的 runAction

xcode - 包含来自 Xcode 上添加的框架的 header