在计数器减少时更新按钮 UIImage iOS

Update a buttons UIImage while a counter decreases iOS

我正在尝试每秒更改 UIButton 的 UIImage,直到计数器达到 0。我尝试了 NSTimer、performSelector withDelay,但我无法使它们中的任何一个工作,大量谷歌搜索提出了不同的选项,但指出我无法在 for 或 while 循环中或 NSTimer 为 运行.

时更新 UI

下面的代码让我很接近,日志显示执行暂停一秒钟并且 imageName 是正确的....但是按钮图像没有改变。

非常感谢任何帮助。

- (void)setTimerCountdownImage {

  NSString *imageWithTime = @"recordTimer";
  UIImage *btnImage = [UIImage imageNamed:@"recordTimer.png"];

  if (_timerDelay != 0) {

    NSLog(@"Timer Delay is:%d", _timerDelay);
    btnImage = [UIImage imageNamed:[[imageWithTime stringByAppendingString:[NSString stringWithFormat:@"%d", _timerDelay]] stringByAppendingString:@".png"]];
    NSLog(@"TimerImage is: %@",[[imageWithTime stringByAppendingString:[NSString stringWithFormat:@"%d", _timerDelay]] stringByAppendingString:@".png"]);
    [_toggleTimerBtn setImage:btnImage forState:UIControlStateNormal];
    [self countDownTimer:_timerDelay];
 
  }
  else {
   [_toggleTimerBtn setImage:btnImage forState:UIControlStateNormal];
  }
}

-(void)countDownTimer:(int) currentDelay {
 _timerDelay = _timerDelay -1;
 NSLog(@"Time to sleep...");
 [NSThread sleepForTimeInterval:1.0];
 [self setTimerCountdownImage];
}

日志输出:

2015-02-17 01:42:22.357 SwingPlane[299:16789] Timer Delay is:5
2015-02-17 01:42:22.358 SwingPlane[299:16789] TimerImage is: recordTimer5.png
2015-02-17 01:42:22.358 SwingPlane[299:16789] Time to sleep...
2015-02-17 01:42:23.360 SwingPlane[299:16789] Timer Delay is:4
2015-02-17 01:42:23.374 SwingPlane[299:16789] TimerImage is: recordTimer4.png
2015-02-17 01:42:23.375 SwingPlane[299:16789] Time to sleep...
2015-02-17 01:42:24.377 SwingPlane[299:16789] Timer Delay is:3
2015-02-17 01:42:24.391 SwingPlane[299:16789] TimerImage is: recordTimer3.png
2015-02-17 01:42:24.392 SwingPlane[299:16789] Time to sleep...
2015-02-17 01:42:25.394 SwingPlane[299:16789] Timer Delay is:2
2015-02-17 01:42:25.408 SwingPlane[299:16789] TimerImage is: recordTimer2.png
2015-02-17 01:42:25.408 SwingPlane[299:16789] Time to sleep...
2015-02-17 01:42:26.411 SwingPlane[299:16789] Timer Delay is:1
2015-02-17 01:42:26.427 SwingPlane[299:16789] TimerImage is: recordTimer1.png
2015-02-17 01:42:26.428 SwingPlane[299:16789] Time to sleep...

记得在主线程上进行所有 UI 更改。并使用 NSTimer!试试这个(我自己没有 运行,如果它不起作用请告诉我):

- (void)startTimerWithDelay:(NSInteger)delay {

    _timerDelay = delay;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(setTimerCountdownImage) userInfo:nil repeats:YES];

}


- (void)setTimerCountdownImage {

    NSString *imageWithTime = @"recordTimer";
    UIImage *btnImage = [UIImage imageNamed:@"recordTimer.png"];

    _timerDelay--;

    if (_timerDelay != 0) {

        btnImage = [UIImage imageNamed:[[imageWithTime stringByAppendingString:[NSString stringWithFormat:@"%d", _timerDelay]] stringByAppendingString:@".png"]];
    } else {

        [_timer invalidate];
    }

    dispatch_async(dispatch_get_main_queue(), ^{
        [_toggleTimerBtn setImage:btnImage forState:UIControlStateNormal];
    });
}

记得在 class 中加上 NSTimer *_timer 否则将不起作用。