更改重复NSTimer的fireDate [英] Changing fireDate of a repeating NSTimer

查看:270
本文介绍了更改重复NSTimer的fireDate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am building an app where I need to stack to two timed events (one within the other). I have one timer that will call a web service every 10 seconds and get new data. Between the web service calls I will animate each object I receive over 10 seconds (to bridge the gap and create a 'real-time' feel).

数据量我从Web服务收到的内容会有所不同,因此我的动画计时器需要在10秒内将动画空间分开。

The amount of data I am receiving from the web service will vary and thus my animation timer needs to vary to space out the animations out over 10 seconds.

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

I am trying to change the timeInterval of my repeating NSTimer but can not get it to change the timeinterval when the next data set comes in.

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;


}


推荐答案

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

The best way I've found to coordinate two timers is to have just one timer, used as a pulse. I would do something like this:

// 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--;
    }
}

唯一要做的就是设置自己。 mapPointsPerFrame。每次获取后执行此操作。如果您的帧间隔是0.5秒并且您每10秒获取一次,那么它的等式如下所示:

The only thing left to do is to set self.mapPointsPerFrame. Do that after each fetch. If your frame interval is 0.5s and you fetch every 10s, then the equation for that looks like this:

// 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的fireDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆