使用 NSTimer 没有延迟 [英] no delay achieved with NSTimer

查看:93
本文介绍了使用 NSTimer 没有延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在每个输出结果之间延迟 1 秒.下面的代码没有延迟,但正在正确处理.为什么在每个 dealCard 之间没有延迟?

I want to delay between each output result for say, 1 second. The code below is not delaying but is processing correctly. Why is it not delaying between each dealCard?

- (IBAction)startPause
{
    if ([self.deal length]>cardNum) {
        timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(dealCard) userInfo:nil repeats:NO];
        [timer fire];
    }
}


- (void) dealCard{
    card.text = [NSString stringWithFormat:@"%i",cardNum+1];
    cardTo.text = [self.deal substringWithRange:(NSRange){(cardNum+self.randCut)%[self.cardList count],1}];
    cardNum=cardNum + 1;
    [self startPause];
}

推荐答案

根据 documentation fire "导致接收者的消息被发送到其目标."这就是它立即触发的原因.

According to the documentation fire "Causes the receiver’s message to be sent to its target." Which is why it's firing instantly.

您的计时器也未安排好,因此它永远不会自行触发.

Your timer is also not scheduled, so it would never fire on it's own.

像这样创建您的计时器,它会创建一个计时器,并在单个语句中对其进行调度.

Create your timer like this instead, which creates a timer, and schedules it in a single statement.

timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                         target:self
                                       selector:@selector(dealCard)
                                       userInfo:nil
                                        repeats:NO];

并完全删除 [timer fire] 行,因为它会在时间间隔结束后自行触发.

And remove the [timer fire] line completely, as it will fire itself after it's time interval is up.

这篇关于使用 NSTimer 没有延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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