While 循环中的 NSTimer [英] a NSTimer in While Loop

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

问题描述

您好!!

我现在正在做以下代码:

The following codes are now I am doing :

BOOL bJobDone = NO ;
bBrickDropDone = NO ; //global var here !!
NSTimer* timer = nil ;

while(bJobDone==NO)
{    

    if(timer == nil)
    {
        timer = [NSTimer scheduledTimerWithTimeInterval:0.3
                                                      target:self
                                                    selector:@selector(dropBrick:)
                                                    userInfo:nil
                                                     repeats:YES];
    }
    if(bBrickDropDone==YES)
    {
        bBrickDropDone = NO ;
        [timer invalidate] ;
        timer = nil ;

        if([self MarkBrickBomb]==YES)
        {
            bJobDone = NO ;
            [self dealBomb] ;
            if([self AllClean]==YES)
            {
                bJobDone = YES ;
                igameidx = igameidx + 1 ;
            }
        }else
        {
            bJobDone = YES ;
        }            
    }//if(bBrickDropDone==YES)

}//while bJobDone==NO

如你所见,定时器调用 dropBrick 函数一次,持续 0.3 秒,如果 bBrickDropDone 最终 = YES(如果发生某些事情,dropBrick 函数将其修改为 YES)它将处理另一个函数 MarkBrickBomb 并继续直到 bJobDone=YES,跳出循环!!

As you can see , the timer call dropBrick function once for 0.3 second , if bBrickDropDone is finally = YES (dropBrick function modify this to YES if something happen) it will process another function MarkBrickBomb and goes on until bJobDone=YES,break out of loop!!

我认为我的代码很糟糕,我不应该在 while 循环中检查 bBrickDropDone 标志,因为效率不高,还耗费大量资源!!

I think my code is lousy , I should not check bBrickDropDone flag in the while loop, because it is not efficient , also cost resource a lot !!

我需要一个定时器来获得一个动画 UIImageView 开关,买还是不喜欢在while循环中检查完成标志(我认为这不好),那我该怎么办在这种情况下 ?我可以得到一个提示吗?

I need a timer to get an animated UIImageView switch , buy Still don't like to check the done flag in while loop(it is not good ,I think) , so what can I do in this case ? Can I get a hint for this ?

还有,对不起我的英语!!

And , sorry for my english !!

推荐答案

计时器永远不能在 bJobDone == NO 时触发,这是因为 NSTimers 被添加到 NSRunLoop 并且只能在等待事件时触发.

The timer can never fire whilst bJobDone == NO, this is because, NSTimers are added to the NSRunLoop and can only fire whilst waiting for an event.

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

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