Objective c 更改重复NSTimer的fireDate

Objective c 更改重复NSTimer的fireDate,objective-c,ios7,nsdate,nstimer,nstimeinterval,Objective C,Ios7,Nsdate,Nstimer,Nstimeinterval,我正在构建一个应用程序,其中我需要堆叠两个定时事件(一个在另一个内)。我有一个计时器,每10秒调用一个web服务并获取新数据。在web服务调用之间,我将在10秒钟内为接收到的每个对象设置动画(以缩小差距并创建“实时”感觉) 我从web服务接收的数据量会有所不同,因此我的动画计时器需要改变,以便在10秒钟内将动画隔开 我正在尝试更改重复NSTimer的时间间隔,但无法让它在下一个数据集进入时更改时间间隔 timerDelay = 9.8/([timerAdditions count]-1); i

我正在构建一个应用程序,其中我需要堆叠两个定时事件(一个在另一个内)。我有一个计时器,每10秒调用一个web服务并获取新数据。在web服务调用之间,我将在10秒钟内为接收到的每个对象设置动画(以缩小差距并创建“实时”感觉)

我从web服务接收的数据量会有所不同,因此我的动画计时器需要改变,以便在10秒钟内将动画隔开

我正在尝试更改重复NSTimer的时间间隔,但无法让它在下一个数据集进入时更改时间间隔

timerDelay = 9.8/([timerAdditions count]-1);

if (!delayTimer) {

    delayTimer =[NSTimer scheduledTimerWithTimeInterval:timerDelay target:self selector:@selector(delayMethod) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:delayTimer forMode:NSRunLoopCommonModes];

} else {

    NSTimeInterval timeInMiliseconds = timerDelay;
    NSDate *myDate =[NSDate dateWithTimeIntervalSinceNow:timeInMiliseconds];
    delayTimer.fireDate = myDate;


}

我发现协调两个计时器的最好方法是只有一个计时器,用作脉冲。我会这样做:

// keep state on when the next fetch should happen
@property(strong,nonatomic) NSDate *fetchTime;

// keep state on how much animation to do per tick
@property(assign,nonatomic) NSInteger mapPointsPerFrame;

// run a single timer at the highest frame-rate you'll need
NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

- (void)timerFired:(NSTimer *)timer {

    // is it time to do a fetch?
    NSDate *now = [NSDate date];
    if ([self.fetchTime laterDate:now] == self.fetchTime) {
        // call a method to start a new fetch
        self.fetchTime = [NSDate dateWithTimeInterval:10 sinceDate:now];
    }

    // either way, do animations if they are needed
    NSInteger mapPoints = self.mapPointsPerFrame;
    while (self.mapPointsToAnimate.count && mapPoints) {
        // call a method to add a map point
        mapPoints--;
    }
}
// just fetched N things and added N objects to self.mapPointsToAnimate
// try to animate them all by the next fetch
self.mapPointsPerFrame = (self.mapPointsToAnimate.count / 20) + 0.5; // 0.5 to round
唯一要做的就是设置self.mapPointsPerFrame。每次抓取后都要这样做。如果帧间隔为0.5s,并且每10s取一次,则其方程如下所示:

// keep state on when the next fetch should happen
@property(strong,nonatomic) NSDate *fetchTime;

// keep state on how much animation to do per tick
@property(assign,nonatomic) NSInteger mapPointsPerFrame;

// run a single timer at the highest frame-rate you'll need
NSTimer *timer =[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];

- (void)timerFired:(NSTimer *)timer {

    // is it time to do a fetch?
    NSDate *now = [NSDate date];
    if ([self.fetchTime laterDate:now] == self.fetchTime) {
        // call a method to start a new fetch
        self.fetchTime = [NSDate dateWithTimeInterval:10 sinceDate:now];
    }

    // either way, do animations if they are needed
    NSInteger mapPoints = self.mapPointsPerFrame;
    while (self.mapPointsToAnimate.count && mapPoints) {
        // call a method to add a map point
        mapPoints--;
    }
}
// just fetched N things and added N objects to self.mapPointsToAnimate
// try to animate them all by the next fetch
self.mapPointsPerFrame = (self.mapPointsToAnimate.count / 20) + 0.5; // 0.5 to round

我模模糊糊地记得,
NSTimer
只应该从它所连接的线程中进行处理。。。还想知道为什么你会对任何与动画相关的东西使用
NSTimer
。@Zach我这么做的时候,发生了一件奇怪的事情,我创建的每个新计时器都不会在任何接近正确时间的地方启动。。。通常提前4到5秒完成。你可以在这里看到帖子-@BradAllred-我正在使用NSTimer在我的MkMapView上启动
addAnnotation
removenotation
方法,因此它实际上并不是在处理动画,而是在计时。实际上,我也尝试过使用GCD、CADisplayLink,无论我如何设置计时器,它们在第二次循环后都会快速启动。谢谢……我对您的代码进行了一些自定义,以这种方式格式化时间效果很好,解决了我的问题。