1

基本的に、私は(とりわけ)次々に繰り返して強調表示したいボタンの配列を持っていますが、その間に遅延があります。簡単な作業のように思えますが、応答性を維持しながら、正常に動作させることができないようです。

私はこれから始めました:

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

しかし、応答がないので、代わりに実行ループを使用してみました。

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.
}

ただし、応答もありません。

これを行うための合理的な方法はありますか?スレッドやNSTimersを使用するのは面倒なようです。

4

2 に答える 2

2

NSTimerはこのタスクに最適です。

タイマーのアクションはx秒ごとに起動します。ここで、xは指定したものです。

重要な点は、これが実行中のスレッドをブロックしないことです。Peterがこの回答のコメントで言ったように、タイマーが別のスレッドで待機していると言ったのは間違っていました。詳細については、コメント内のリンクを参照してください。

于 2010-02-09T20:03:21.323 に答える
1

気にしないで、Jasarien は正しかった、NSTimer完全に適しています。

- (void)tapButtons:(NSArray *)buttons
{
    const NSTimeInterval waitInterval = 0.5; // Wait 500 milliseconds between each button.
    NSTimeInterval nextInterval = waitInterval;
    for (MyButton *button in buttons) {
        [NSTimer scheduledTimerWithTimeInterval:nextInterval
                                         target:self
                                       selector:@selector(tapButtonForTimer:)
                                       userInfo:button
                                        repeats:NO];
        nextInterval += waitInterval;
    }
}

- (void)tapButtonForTimer:(NSTimer *)timer
{
    MyButton *button = [timer userInfo];
    [button highlight];
    [button doStuff];
}
于 2010-02-09T20:12:55.233 に答える