在iPhone中暂停秒表计时器?
我对iPhone中的NSTimer是新手。我做了一个秒表。现在我想在两者之间暂停一下 并且还要做恢复暂停。我的代码如下在iPhone中暂停秒表计时器?,iphone,objective-c,xcode,Iphone,Objective C,Xcode,我对iPhone中的NSTimer是新手。我做了一个秒表。现在我想在两者之间暂停一下 并且还要做恢复暂停。我的代码如下 我怎样才能暂停时间 我怎样才能从时间停止的地方继续 开始时间: startDate = [[NSDate date]retain]; stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 t
- 我怎样才能暂停时间
- 我怎样才能从时间停止的地方继续
startDate = [[NSDate date]retain];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
停止时间:
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
更新时间:
(void)updateTimer
{
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
stopWatchLabel.text = timeString;
[dateFormatter release];
}
NSTimer
没有暂停或恢复方法。您可以制作两种类型的计时器,一种只执行一次,另一种重复执行。例如:
创建一个计时器,该计时器将每秒进入回调myMethod
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(myMethod:)
userInfo:nil
repeats:YES];
你可能会选择这一个作为你的目的,在你的课堂上你应该保持一些
BOOL pausevariable
并在回调myMethod中执行以下操作:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
在代码中更新暂停的位置。要停止计时器并释放内存,请调用
[myTimer invalidate];
希望这能有所帮助。我创建了一些额外的变量来帮助跟踪经过的时间:
BOOL watchStart代码>
NSTimeInterval pauseTimeInterval代码>
iAction
),并且在UpdateTimeInterval
中,我将时间间隔设置为PauseTimeiInterval
守则的关键部分是:
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]
该行将把额外的时间间隔
添加到开始日期
,以计算暂停的时间。这有点像是在时间上“退一步”加上之前记录的时间间隔
-(void) updateTimer {
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];
_stopWatchLabel.text = timeString;
_pauseTimeInterval = timeInterval;
}
-(IBAction)btnStartPause:(id)sender {
if(_watchStart == NO) {
_watchStart = YES;
_startDate = [NSDate date];
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))];
_stopWatchTime = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
else {
_watchStart = NO;
[_stopWatchTime invalidate];
_stopWatchTime = nil;
[self updateTimer];
}
上述示例的小修正:
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))];
在末尾添加retain:
_startDate = [[_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]retain];
这将使事情变得更好:-)您可以使用NSTimeInterval而不是计时器。我有一个暂停和停止计时器的功能代码
@interface PerformBenchmarksViewController () {
int currMinute;
int currSecond;
int currHour;
int mins;
NSDate *startDate;
NSTimeInterval secondsAlreadyRun;
}
@end
- (void)viewDidLoad
{
[super viewDidLoad];
running = false;
}
- (IBAction)StartTimer:(id)sender {
if(running == false) {
//start timer
running = true;
startDate = [[NSDate alloc] init];
startTime = [NSDate timeIntervalSinceReferenceDate];
[sender setTitle:@"Pause" forState:UIControlStateNormal];
[self updateTime];
}
else {
//pause timer
secondsAlreadyRun += [[NSDate date] timeIntervalSinceDate:startDate];
startDate = [[NSDate alloc] init];
[sender setTitle:@"Start" forState:UIControlStateNormal];
running = false;
}
}
- (void)updateTime {
if(running == false) return;
//calculate elapsed time
NSTimeInterval currentTime = [NSDate timeIntervalSinceReferenceDate];
NSTimeInterval elapsed = secondsAlreadyRun + currentTime - startTime;
// extract out the minutes, seconds, and hours of seconds from elapsed time:
int hours = (int)(mins / 60.0);
elapsed -= hours * 60;
mins = (int)(elapsed / 60.0);
elapsed -= mins * 60;
int secs = (int) (elapsed);
//update our lable using the format of 00:00:00
timerLabel.text = [NSString stringWithFormat:@"%02u:%02u:%02u", hours, mins, secs];
//call uptadeTime again after 1 second
[self performSelector:@selector(updateTime) withObject:self afterDelay:1];
}
希望这会有所帮助。谢谢
-(void)startStopwatch
{
//initialize the timer HUD
[self setSeconds:second];
// [self.hud.stopwatch setSeconds:_secondsLeft];
//schedule a new timer
_timer = [NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(tick:)
userInfo:nil
repeats:YES];
}
-(void)pause{
[_timer invalidate];
}
-(void)resume {
[_timer isValid];
}
使用此代码,我们可以暂停和恢复计时器朋友我成功地在iOs计时器应用程序中实现了暂停和恢复功能,代码如下
- (IBAction)pauseT:(id)sender {
se = sec;
mi = min;
ho = hour;
[timer invalidate];
}
}
希望这对你们有用。我知道NSTimer没有暂停和恢复方法。但我不知道如何暂停,所以有没有暂停计时器的逻辑。顺便说一句,谢谢你的夸奖。那你为什么说“我对iPhone中的NSTimer是新来的。我做了一个秒表。现在我想在两者之间暂停一段时间,还想继续暂停”。)实用易懂,解决了我的问题,谢谢分享!然而,当从经过的时间中提取小时数时,我认为应该是:inthours=(int)(经过的/3600.00);已用时间-=小时*3600代码>否则60分钟后会出错。
- (IBAction)resumeT:(id)sender {
sec = se;
min = mi;
hour = ho;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];