iOS 几种简单的延迟操作

作者: HOULI | 来源:发表于2016-07-26 22:37 被阅读90次

    最近在做项目中碰到需要做个延时操作,于是把这个几个简单方法记录下来以便以后使用方便; 如果有更好的方法欢迎添加一起完善;

    1、NSimer

     //1秒后执行
    NSTimer  *_timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(_doTimer:) userInfo:nil repeats:NO];
    

    2、系统

       //延迟1s执行
     [self performSelector:@selector(_doTimer:) withObject:nil afterDelay:1];
    

    3、GCD 延迟

    /**
     *  异步执行 好处在于经度高
     *  参数1:延时的时间 dispatch_time 生成时间 纳秒为计数单位 
     *  参数2:队列
     *  参数3:任务   并且异步执行
     *  block 里边是延迟后执行的方法
     */
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self _task];
    });

    相关文章

      网友评论

        本文标题:iOS 几种简单的延迟操作

        本文链接:https://www.haomeiwen.com/subject/vmfojttx.html