gpt4 book ai didi

iphone - NSTimer : How to change the scheduled method without having delay?

转载 作者:行者123 更新时间:2023-11-29 04:51:02 24 4
gpt4 key购买 nike

在我的 iPhone TIMER 应用程序中,

经过一段时间后,我需要更改预定的方法......

-(void)startTimerAction
{
NSLog(@"Start timer Action");
NSLog(@"Time is Over %@",isTimeOver?@"YES":@"NO");

if(!isTimeOver)
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(ShowActicity) userInfo:nil repeats:YES];
}
else if(isTimeOver)
{
timer_main = [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(continueClock) userInfo:nil repeats:YES];
}

}

所以,为了停止并重新安排它,我正在这样做......以某种方式......

isTimeOver=YES;
[timer_main invalidate];
timer_main=nil;
[self startTimerAction];

为此,需要一些时间延迟或不需要的间隔才能在新方法中重新安排计时器....

怎样才能顺利完成......

我只是想用另一种方法重新安排计时器而不造成延迟......

现在我正在这样做来修复它......

      NSTimeInterval x=[[timer_main fireDate] timeIntervalSinceNow];
[timer_main invalidate];
timer_main=nil;
sleep(x);
totalCount++;
seconds++;
if(seconds>59)
{
minutes++;
seconds=0;
}

[self startTimerAction];

谢谢...

最佳答案

您可以schedule repeating timers with a firedate :

NSTimer *timer = [[NSTimer alloc] initWithFireDate:fireDate
interval:interval
target:self
selector:@selector(foo)
userInfo:nil
repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];

关于iphone - NSTimer : How to change the scheduled method without having delay?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8834857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com