你好 我开发小gameApp恢复的NSTimer。 我需要暂停计时器,当用户转到另一个视图[说设置视图]。 当用户回到那个视图时,我需要恢复定时器。如何暂停和iphone
有谁能够解决这个问题...
在此先感谢...
你好 我开发小gameApp恢复的NSTimer。 我需要暂停计时器,当用户转到另一个视图[说设置视图]。 当用户回到那个视图时,我需要恢复定时器。如何暂停和iphone
有谁能够解决这个问题...
在此先感谢...
您不能暂停计时器。但是,当用户转到设置视图时,您可以保存计时器的fireDate
以及当前日期。在此之后,您使计时器无效并让用户完成他/她的工作。 一旦他/她切换回游戏,您将创建一个新的计时器对象,并将启动日期设置为旧启动日期加上用户在菜单中的时间(oldTime + currentTime)。
您不能暂停一个NSTimer。但是,您可以使其无效并在需要时创建一个新的。
感谢,但我不能因为无效时,我去另外的看法,回来那么就应该从那里被停止启动。 – 2010-11-11 04:42:20
on Start -
startNewCapture = [NSDate date];
on Pause -
captureSessionPauseDate = [NSDate date];
captureSessionTimeInterval = [captureSessionPauseDate timeIntervalSinceDate:startNewCapture];
on Resume -
NSDate *dateNow = [NSDate date];
startNewCapture = [NSDate dateWithTimeInterval:-captureSessionTimeInterval sinceDate:dateNow];
NSTimer不会让您暂停它。但是,通过几个简单的变量,您可以自己创建效果:
NSTimer *timer;
double timerInterval = 10.0;
double timerElapsed = 0.0;
NSDate *timerStarted;
-(void) startTimer {
timer = [NSTimer scheduledTimerWithTimeInterval:(timerInterval - timerElapsed) target:self selector:@selector(fired) userInfo:nil repeats:NO];
timerStarted = [NSDate date];
}
-(void) fired {
[timer invalidate];
timer = nil;
timerElapsed = 0.0;
[self startTimer];
// react to timer event here
}
-(void) pauseTimer {
[timer invalidate];
timer = nil;
timerElapsed = [[NSDate date] timeIntervalSinceDate:timerStarted];
}
这对我来说工作得非常好。
您可以使用此代码来实现暂停和恢复功能中的NSTimer
//=========new timer update method=============
-(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];
lblMessage.text = timeString;
pauseTimeInterval = timeInterval;
}
-(IBAction) startBtnPressed:(id)sender
{
//=============================new update with start pause==================
if(running == NO) {
running = YES;
startDate = [NSDate date] ;
startDate = [[startDate dateByAddingTimeInterval:((-1)*(pauseTimeInterval))] retain];
stopWatchTimer = [NSTimer scheduledTimerWithTimeInterval:1.0/10.0 target:self selector:@selector(updateTimer) userInfo:nil repeats:YES];
}
else {
running = NO;
[stopWatchTimer invalidate];
stopWatchTimer = nil;
[self updateTimer];
}
}
宣布在.h文件中的NSDate的startDate;
NSTimeInterval pauseTimeinterval; 并在viewdidload中pausetimeInterval = 0.0;好运
好工作:) – MTahir 2014-05-11 14:19:15
- (IBAction)pauseResumeTimer:(id)sender {
if (timerRunning == NO) {
timerRunning = YES;
[pauseTimerBtn setTitle:@"Resume" forState:UIControlStateNormal];
NSString *stringVal = [NSString stringWithFormat:@"%@",timeTxt.text];
stringVal = [stringVal stringByReplacingOccurrencesOfString:@":" withString:@"."];
float tempFloatVal = [stringVal floatValue];
int minuteValue = floorf(tempFloatVal);
float tempSecVal = [stringVal floatValue] - floorf(tempFloatVal);
int secondVal = tempSecVal*100;
minuteValue = minuteValue*60;
oldTimeValue = minuteValue + secondVal;
[timer invalidate];
timer = nil;
}
else
{
timerRunning = NO;
[pauseTimerBtn setTitle:@"Pause" forState:UIControlStateNormal];
startDate = [NSDate date];
timer = [NSTimer scheduledTimerWithTimeInterval:0.25 target:self selector:@selector(timer:) userInfo:nil repeats:YES];
}
}
- (void)runTimer:(NSTimer *)timer {
NSInteger secondsAtStart = (NSInteger)[[NSDate date] timeIntervalSinceDate:startDate];
secondsAtStart = secondsAtStart + oldTimeValue;
NSInteger seconds = secondsAtStart % 60;
NSInteger minutes = (secondsAtStart/60) % 60;
NSInteger hours = secondsAtStart/(60 * 60);
NSString *result = nil;
result = [NSString stringWithFormat:@"%02ld:%02ld",(long)minutes,(long)seconds];
timeTxt.text = result;
}
你最终想出来的?我看到你说过,当你进入另一个视图时,你不能使你的计时器无效。你能解释一下你的意思吗? NSTimers不能被暂停,模拟暂停它们的方法通常会使它们失效。然后您可以通过创建一个新的定时器来模拟“解除暂停”,然后再重新启动。这将模拟暂停和未暂停的计时器。
对于谁愿意跟一个潜在的更方便的方法的人,我写了一个控制器类,可以方便地处理暂停和取消暂停计时器。你可以在这里找到它:https://github.com/LianaChu/LCPausableTimer
您可以使用,我写来创建新的计时器控制器类,然后你可以暂停,并通过电话取消暂停计时器上的控制器类的方法“pauseTimer”和“unpauseTimer”。
我将不胜感激,怎么样,你使用它,什么样的变化,你想看看,或者你想我添加任何特征的任何意见或建议。请不要犹豫,在这里回复您的意见,或张贴在Github页面的问题标签上。
谢谢,但我是iphone新手。你能否给我提供一些演示样本,以便我可以轻松地通过它。 – 2010-11-11 04:47:12
@JustSid,你能为此提供示例代码吗?..我知道这已经过了几年了,但对我来说,创建一个新的问题对我来说是多余的。 :) – Pangu 2016-03-07 18:19:06