ios - 更新按钮 UIImage 而计数器减少 iOS

标签 ios objective-c nstimer sleep

我试图每秒更改一个 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];
    });
}

请记住将 NSTimer *_timer 添加到类中,否则它将无法工作。

关于ios - 更新按钮 UIImage 而计数器减少 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28553558/

相关文章:

ios - 如何在循环迭代中创建间隔

objective-c - 如何避免在动画(completionBlock)和非动画代码之间出现重复代码

ios - UITableView contentOffset 行为异常

iphone - self 和 super 的区别

ios - 如何检查某个对象是否存在于 NSMutableArray 的某个索引处?

ios - PopViewController 动画 :YES when tap on UIAlertView make Keyboard appear in parentVC

ios - 没有初始延迟的 NSTimer

ios - 添加计时器以更新 UIView 背景颜色

ios - 将导航 Controller 限制为一组 UIViewController

ios - iOS7中基于用户当前位置区域监控、删除监控区域