While 循环中的 NSTimer
问候!
我现在正在做的代码如下:
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,跳出循环!
我认为我的代码很糟糕,我不应该在 while 循环中检查 bBrickDropDone 标志, 因为效率不高,而且还耗费很多资源!!
我需要一个计时器来获得动画 UIImageView 开关,买了还是不喜欢 检查 while 循环中的完成标志(我认为这不好),那么我该怎么办 在这种情况下 ?我可以得到这方面的提示吗?
而且,对不起我的英语!
Greeting !!
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
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!!
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 !!
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 !!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当 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.