Change NSTimer interval after a certain number of fires

前端 未结 3 1417
死守一世寂寞
死守一世寂寞 2021-01-26 14:48

In the following code, the NSTimer interval is set at 1 second between each picture. My goal is to change the interval after the first two pictures, hello.png and b

3条回答
  •  半阙折子戏
    2021-01-26 15:24

    The best/simpliest way :

        -(void)Timer{
    
        // Do what you want here
    
      // modify the frequency value if you want.  
            [NSTimer scheduledTimerWithTimeInterval:frequence target:self selector:@selector(Timer) userInfo:nil repeats:NO];
        }
    

    Frequence is a variable that contains the interval. You just need to call Timer method by yourself the first time ;)

    For you, it would give

        -(void)changeImage{
            if (counter == self.images.count - 1 )
                counter = 0;
            else
                counter ++;
    
            frequence = (counter < 2)?1:4;
    
            self.animationImageView.image = self.images[counter];
            [NSTimer scheduledTimerWithTimeInterval:frequence target:self selector:@selector(changeImage) userInfo:nil repeats:NO];
        }
    

提交回复
热议问题