美文网首页
关于OC中的几种代码延迟执行模式

关于OC中的几种代码延迟执行模式

作者: GrayMantis | 来源:发表于2017-04-28 00:30 被阅读0次

    第一种:

    [UIView animateWithDuration:3 delay:3 options:1 animations:^{

    self.btn.transform = CGAffineTransformMakeTranslation(300, 400);

    } completion:^(BOOL finished) {

    NSLog(@"view animation结束");

    }];//不会阻塞线程,animations  block中的代码对于是支持animation的代码,才会有延时效果,

    对于不支持animation的代码 则 不会有延时效果

    第二种:

    [NSThread sleepForTimeInterval:3];//阻塞线程,浪费性能 ,一般不推荐用

    第三种:最常用

    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

    });//定制了延时执行的任务,不会阻塞线程,效率较高(推荐使用)

    第四种:

    [self performSelector:@selector(test) withObject:nil afterDelay:3];//不阻塞线程

    相关文章

      网友评论

          本文标题:关于OC中的几种代码延迟执行模式

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