在一定数量的火灾后更改 NSTimer 间隔

Change NSTimer interval after a certain number of fires

在下面的代码中,每张图片之间的NSTimer间隔设置为1秒。我的目标是将前两张图片 hello.png 和 bye.png 之后的间隔更改为 4 秒。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png",@"bye again"];

    self.images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
    self.animationImageView.image = self.images[0];
    [self.view addSubview:self.animationImageView];
    self.animationImageView.userInteractionEnabled = TRUE;



    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
}


-(void)changeImage:(NSTimer *) timer {
    if (counter == self.images.count - 1 ) {
        counter = 0;
    }else {
        counter ++;
    }
    self.animationImageView.image = self.images[counter];
}

my goal is to change the interval

您不能以任何方式更改计时器。要更改触发间隔,请使计时器无效并销毁并使用新间隔创建一个新计时器。

要做到 那个,您需要保留对计时器的引用,作为视图控制器的 属性(这是您未能做到的在你的实现中做 - 你将无法停止计时器,如果你的视图控制器不存在,你就会崩溃,否则你的视图控制器会因为计时器保留它而泄漏)。

将计时器对象设为成员变量。最初将动画时间设置为 1 秒。在回调中使计时器无效并根据计数器创建一个 1 或 4 秒的新计时器。

@interface ViewController ()
@property (strong,nonatomic) NSMutableArray *images;
@property (strong,nonatomic) UIImageView *animationImageView;
{
    NSTimer *_timer;
}
@end

@implementation ViewController {
    NSInteger counter;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *imageNames = @[@"hello.png",@"bye.png",@"helloagain.png",@"bye again"];

    self.images = [[NSMutableArray alloc] init];
    for (int i = 0; i < imageNames.count; i++) {
        [self.images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
    }

    self.animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(60, 95, 86, 90)];
    self.animationImageView.image = self.images[0];
    [self.view addSubview:self.animationImageView];
    self.animationImageView.userInteractionEnabled = TRUE;

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
    singleTap.numberOfTapsRequired = 1;
    singleTap.numberOfTouchesRequired = 1;
    [self.animationImageView addGestureRecognizer:singleTap];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
}


-(void)changeImage:(NSTimer *) timer {
     [_timer invalidate];
    if (counter == self.images.count - 1 ) {
        counter = 0;
    }else {
        counter ++;
    }
    if(counter == 0 || counter == 1)
    {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
    }
    else if(counter == 2 || counter == 3)
    {
        _timer = [NSTimer scheduledTimerWithTimeInterval:4 target:self selector:@selector(changeImage:) userInfo:nil repeats:YES];
    }
    self.animationImageView.image = self.images[counter];
}

best/simpliest 方式:

    -(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];
    }

频率是一个包含间隔的变量。 您只需要在第一次自己调用 Timer 方法即可;)

对你来说,它会给

    -(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];
    }