Objective-C delay action with blocks

后端 未结 4 891
眼角桃花
眼角桃花 2020-12-08 00:12

I know that there are several ways of delaying an action in Objective-C like:

performSelector:withObject:afterDelay:

or using NSTimer

4条回答
  •  暖寄归人
    2020-12-08 00:40

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

提交回复
热议问题