2016年4月29日 星期五

iOS tips - add delay

寫app時,不管是為了加上一些效果,或者是解一些奇怪的bug,常會需要加上delay然後來處理我們之後想要做的code,下面這段code就是delay 2秒然後處理另外的task

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];
});

沒有留言:

張貼留言