gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:27:51 26 4
gpt4 key购买 nike

我试图每秒更改一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com