我知道在 Objective-C 中有几种延迟 Action 的方法,例如:
performSelector:withObject:afterDelay:
或使用 NSTimer
。
但是有一种奇特的东西叫做积木,你可以在其中做这样的事情:
[UIView animateWithDuration:1.50 delay:0 options:(UIViewAnimationOptionCurveEaseOut|UIViewAnimationOptionBeginFromCurrentState) animations:^{
}completion:^(BOOL finished){
}];
不幸的是,这种方法只适用于动画。
如何在 one 方法 中使用 block 创建延迟,这样我就不必使用所有这些 @selectors
并且无需创建 新的分离方法?谢谢!
最佳答案
使用 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];
});
关于ios - 带有 block 的 Objective-C 延迟 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15413014/