函数sleep()和[[NSRunLoop currentRunLoop] runUntilDate]的使用差异 [英] Difference in usage of function sleep() and [[NSRunLoop currentRunLoop] runUntilDate]

查看:536
本文介绍了函数sleep()和[[NSRunLoop currentRunLoop] runUntilDate]的使用差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

在第一个代码中,我调用一个创建动画的函数。我以一定的时间间隔执行此操作:

In the first one i call a function which creates an animation. i do that with a certain time interval:

start:;

[self animationMethod];

[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:3]];
//sleep(3);

goto start;

在第二个我制作动画

- (void)animationMethod
 {
  CAKeyframeAnimation *myAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
CGMutablePathRef curvedPath             = CGPathCreateMutable();

CGPathMoveToPoint(curvedPath, NULL, start.x, start.y);
CGPathAddCurveToPoint(curvedPath, NULL, fcp.x, fcp.y, scp.x , scp.y, end.x, end.y); 

myAnimation.path                = curvedPath;
myAnimation.duration            = flight_duration;
myAnimation.removedOnCompletion = NO;
myAnimation.delegate            = self;  
myAnimation.fillMode            = kCAFillModeForwards;

[myAnimation setValue:identifier forKey:@"id"];

[flyingBug addAnimation:myAnimation forKey:@"bug_flight"];

CGPathRelease(curvedPath);
 }

,第三个是委托方法,用于检查一切是否正常ok:

and the third one is a delegate method which i use to check everything is going on ok:

- (void)animationDidStart:(CAAnimation *)anim
{

    NSLog(@"ANIMATION DID START");
}

所以当我使用 NSRunLoop 它可以正常工作,调用委托方法,但如果我尝试使用 sleep(3)函数,则不会调用委托方法。

So when i use NSRunLoop it works all right, the delegate method is called, but if i try to use with sleep(3) function then the delegate method doesn't get called.

我的问题:

1)你能解释一下NSRunLoop和sleep()之间的区别吗?为什么在使用sleep()时不调用委托方法?

1) Can you please explain the difference between NSRunLoop and sleep()? Why the delegate method isn't called when sleep() is used?

2)也许第三种可能的方法更好用?

2) Maybe there is a third possible method which is better in usage?

推荐答案

NSRunLoop更好,因为它允许runloop在等待时响应事件。如果你只是睡觉你的线程你的应用程序将阻止即使事件到达(如你正在等待的网络响应)。

NSRunLoop is better because it allows the runloop to respond to events while you wait. If you just sleep your thread your app will block even if events arrive (like the network responses you are waiting for).

这篇关于函数sleep()和[[NSRunLoop currentRunLoop] runUntilDate]的使用差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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