I am trying to change a UIButton's UIImage every second, until a counter reaches 0. I have tried NSTimer, performSelector withDelay but I could not get either of those to work, plenty of googling came up with different options but pointed to the fact I can't update the UI when in a for or while loop, or when an NSTimer is running.
The code below gets me close, the logs show execution is pausing for one second and the imageName is correct....but the button image does not change.
Any help greatly appreciated.
- (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];
}
Output from logs:
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...