如何延迟,同时保留响应的GUI上Cocoa Touch? [英] How to delay while retaining a responsive GUI on Cocoa Touch?

查看:88
本文介绍了如何延迟,同时保留响应的GUI上Cocoa Touch?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个按钮数组,我想迭代和突出显示(其他事物)一个接一个,之间的延迟。看起来像一个容易的任务,但我似乎不能设法让它工作干净,同时仍然响应。

Basically, I have an array of buttons I want to iterate and highlight (among other things) one after another, with a delay in-between. Seems like an easy task, but I can't seem to manage to get it to work cleanly while still being responsive.

我开始这个:

for MyButton *button in buttons {
    [button highlight];
    [button doStuff];
    usleep(800000); // Wait 800 milliseconds.
}

但是没有反应,所以我试图使用运行循环。 p>

But it is unresponsive, so I tried using the run loop instead.

void delayWithRunLoop(NSTimeInterval interval)
{
    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:interval];
    [[NSRunLoop currentRunLoop] runUntilDate:date];
}

for MyButton *button in buttons {
    [button highlight];
    [button doStuff];
    delayWithRunLoop(0.8); // Wait 800 milliseconds.
}

但是,它也无响应。

有什么合理的方法吗?使用线程或 NSTimer s。似乎很麻烦。

Is there any reasonable way to do this? It seems cumbersome to use threads or NSTimers.

推荐答案

完美的这个任务。

定时器的动作会在x秒后触发,其中x是您指定的。

The timer's action will fire ever x seconds, where x is what you specify.

这不阻塞它运行的线程。正如彼得在对这个答案的评论中所说的,我不正确地说计时器在一个单独的线程上等待。详情请参阅评论中的连结。

The salient point is that this doesn't block the thread it runs on. As Peter said in the comments for this answer, I was incorrect in saying the timer waits on a separate thread. See the link in the comment for details.

这篇关于如何延迟,同时保留响应的GUI上Cocoa Touch?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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