gpt4 book ai didi

objective-c - Xcode Objective-C | iOS : delay function/NSTimer help?

转载 作者:太空狗 更新时间:2023-10-30 03:08:40 25 4
gpt4 key购买 nike

所以我正在开发我的第一个 iOS 应用程序,我需要帮助..

现在的程序很简单,我有大约 9 个按钮,当我按下第一个按钮或任何按钮时,我只希望第一个按钮突出显示 60 毫秒,取消突出显示,第二个按钮突出显示,等待 60 毫秒,取消突出显示等等其余按钮亮起,因此它看起来像一个移动的 LED。

我已经尝试过 sleep/usleep,但是一旦 sleep 持续时间结束,它似乎就会一起跳过突出显示/取消突出显示。

例如:

- (void) button_circleBusy:(id)sender{
firstButton.enabled = NO;
sleep(1);
firstButton.enabled = YES;

其余按钮依此类推。它确实会延迟,但不会延迟“firstButton.enabled = NO;”。每个按钮的“禁用状态”都有一张图片,但我从未见过。

任何帮助表示赞赏!我研究了 NSTimer,但我不确定如何实现它。

谢谢。

-保罗

最佳答案

sleep 不起作用,因为只有在您的主线程返回系统后才能更新显示。 NSTimer 是要走的路。为此,您需要实现定时器调用的方法来更改按钮。一个例子:

- (void)button_circleBusy:(id)sender {
firstButton.enabled = NO;
// 60 milliseconds is .06 seconds
[NSTimer scheduledTimerWithTimeInterval:.06 target:self selector:@selector(goToSecondButton:) userInfo:nil repeats:NO];
}
- (void)goToSecondButton:(id)sender {
firstButton.enabled = YES;
secondButton.enabled = NO;
[NSTimer scheduledTimerWithTimeInterval:.06 target:self selector:@selector(goToThirdButton:) userInfo:nil repeats:NO];
}
...

关于objective-c - Xcode Objective-C | iOS : delay function/NSTimer help?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5998764/

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