xcode 暂停在 iPhone 中停止手表计时器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7849644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pause Stop watch timer in iPhone?
提问by Rishabh
I am new to NSTimer in iPhone. i made a stop watch. now i want to pause time in between and also want to do resume pause. my code is given bellow.
我是 iPhone 中 NSTimer 的新手。我做了一个秒表。现在我想在两者之间暂停时间,也想恢复暂停。我的代码如下。
- How can I pause time?
- How can i resume from where the time is stop?
- 如何暂停时间?
- 我怎样才能从时间停止的地方恢复?
Start Time:
开始时间:
startDate = [[NSDate date]retain];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0
target:self
selector:@selector(updateTimer)
userInfo:nil
repeats:YES];
Stop Time:
停止时间:
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
Update Time:
更新时间:
(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];
}
回答by Muhammad Aamir Ali
You can use NSTimeInterval instead of timer. I have a functional code to pause and stop the timer.
您可以使用 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];
}
Hope this will help. Thanks
希望这会有所帮助。谢谢
回答by Tha Leang
I created some extra variables to help keep track on the elapsed time:
我创建了一些额外的变量来帮助跟踪经过的时间:
BOOL watchStart;
NSTimeInterval pauseTimeInterval;
BOOL watchStart;
NSTimeInterval pauseTimeInterval;
I set watchStart = NO and pauseTimeInterval = 0.0.
我设置 watchStart = NO 和 pauseTimeInterval = 0.0。
I used watchStart to check if its a start or pause condition (for my IBAction
), and in updateTimer
, I set the timeInterval
to pauseTimeInterval
.
我用watchStart检查其开始或暂停状态(对我的IBAction
),并且在updateTimer
我设定的是timeInterval
要pauseTimeInterval
。
The crucial part of the code is:
代码的关键部分是:
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]
That line will add the extra timeInterval
to the startDate
for calculation of the time that was paused. It's kind of like "stepping back" in time to add in the interval that was previously recorded.
该行将额外添加timeInterval
到startDate
暂停时间的计算中。这有点像及时“退回”以添加先前记录的间隔。
-(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];
}
回答by Gabriel
NSTimer
does not have pause or resume methods. You can make 2 types of timers, one that implements only once and the second, that repeats. Example:
NSTimer
没有暂停或恢复方法。您可以制作两种类型的计时器,一种只执行一次,第二种则重复执行。例子:
Creates a timer that will enter callback myMethod each second.
创建一个计时器,该计时器将每秒进入回调 myMethod。
NSTimer *myTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(myMethod:)
userInfo:nil
repeats:YES];
You probably will choose this one for your purpose where in your class you should maintain some
您可能会出于您的目的选择这个,在您的班级中您应该维护一些
BOOL pausevariable
and in the callback myMethod do the following:
BOOL pausevariable
并在回调 myMethod 中执行以下操作:
- (void) myMethod:(NSTimer *) aTimer
{
if (!pause) {
// do something
// update your GUI
}
}
where you update pause in your code. To stop the timer and release memory, call
您在代码中更新 pause 的位置。要停止计时器并释放内存,请调用
[myTimer invalidate];
hope this helped.
希望这有帮助。
回答by user1344213
Small correction for the above example:
上面例子的小修正:
_startDate = [_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))];
Add retain @ the end :
在末尾添加保留@:
_startDate = [[_startDate dateByAddingTimeInterval:((-1)*(_pauseTimeInterval))]retain];
This will make things better :-)
这会让事情变得更好:-)
回答by user1344213
-(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];
}
using this code we can pause and resume the timer
使用此代码我们可以暂停和恢复计时器
回答by user3490803
Friends i was successful in implementing pause and resume function to iOs timer app, the code is as follows
朋友我成功实现了iOs定时器app的暂停和恢复功能,代码如下
- (IBAction)pauseT:(id)sender {
se = sec;
mi = min;
ho = hour;
[timer invalidate];
}
}
- (IBAction)resumeT:(id)sender {
sec = se;
min = mi;
hour = ho;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countDown) userInfo:nil repeats:YES];
}
}
Hope this works for you guys..
希望这对你们有用..