使用块的Objective-C延迟动作 [英] Objective-C delay action with blocks
本文介绍了使用块的Objective-C延迟动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道在Objective-C中有几种延迟动作的方法,如:
I know that there are several ways of delaying an action in Objective-C like:
performSelector:withObject:afterDelay:
或使用 NSTimer
。
但是有一个很棒的东西叫做积木,你可以做这样的事情:
But there is such a fancy thing called blocks where you can do something like this:
[UIView animateWithDuration:1.50 delay:0 options:(UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState) animations:^{
}completion:^(BOOL finished){
}];
不幸的是,此方法仅适用于动画制作。
Unfortunately, this method applies only to animating things.
如何在一个方法中使用块创建延迟,这样我就不必使用所有 @selectors
而无需创建新的单独方法?谢谢!
How can I create a delay with a block in one method so I don't have to use all those @selectors
and without the need to create a new separate method? Thanks!
推荐答案
使用dispatch_after:
use dispatch_after:
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//code to be executed on the main queue after delay
[self doSometingWithObject:obj1 andAnotherObject:obj2];
});
这篇关于使用块的Objective-C延迟动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文