Xcode Objective-C | iOS:延迟功能/ NSTimer帮助? [英] Xcode Objective-C | iOS: delay function / NSTimer help?

查看:164
本文介绍了Xcode Objective-C | iOS:延迟功能/ NSTimer帮助?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I'm developing my first iOS application and I need help..

现在的简单程序,我有大约9个按钮,当我按下第一个按钮或任何按钮,我只想要第一个按钮突出显示60毫秒,不亮,第二个按钮高亮,等待60毫秒,不亮等等其他按钮所以它看起来像一个移动的LED。

Simple program for now, I have about 9 buttons and when I press the first button, or any button, I just want the first button to highlight for 60 milliseconds, unhighlight, second button highlights, wait 60 milliseconds, unhighlight and so on for the rest of the buttons so it looks like a moving LED.

我看起来已经尝试过睡眠/睡眠,但是一旦睡眠持续时间结束,它似乎就会一起跳过高亮/不亮。

I've looked tried sleep/usleep but once that sleep duration is done it seems like it skips the highlight/unhighlight all together.

例如:

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

以及其他按钮等等。它会延迟,但它不会延迟firstButton.enabled = NO;。我为每个按钮的禁用状态画了一张照片,我从来没有看到它。

and so on for the rest of the buttons. It DOES delay, but it doesn't delay the "firstButton.enabled = NO;". I have a picture for each button's "disabled state" and I never see it.

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

Any help's appreciated! I've looked into NSTimer but I was unsure on how to implement it.

谢谢。

-Paul

推荐答案

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

sleep doesn't work because the display can only be updated after your main thread returns to the system. NSTimer is the way to go. To do this, you need to implement methods which will be called by the timer to change the buttons. An example:

- (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];
}
...

这篇关于Xcode Objective-C | iOS:延迟功能/ NSTimer帮助?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆