延迟执行

1.performSelector

[self performSelector:@selector(doSomeThing) withObject:self afterDelay:2];
/**
 *  延迟执行
 *
 *  @param aSelector  方法名称
 *  @param anArgument 要传递的参数,如果无参数,就设为nil
 *  @param delay      延迟的时间
 */
- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay{}

2.GCD

int64_t delayInSeconds = 10.0;      // 延迟的时间
/*
 *@parameter 1,时间参照,从此刻开始计时
 *@parameter 2,延时多久,此处为秒级,还有纳秒等。10ull * NSEC_PER_MSEC
 */
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    // do something
});

3.每一秒执行一次 (重复性)

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testTimer) userInfo:nil repeats:YES];

你可能感兴趣的:(延迟执行)