线程延迟执行

//touchesEnded全屏点击的一个方法
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//    //这个相当于睡眠的作用 会卡死主线程 主线程内所有东西都会睡三秒 所以我们不适用
//    [NSThread sleepForTimeInterval:3];
    NSLog(@"下载图片------");
    //把一张图片延迟三秒后执行 在下面add:方法里已经有了  所有只需要用一个UIImage来接收一下图片就可以了
    [self performSelector:@selector(add:) withObject:@"2548.jpg" afterDelay:3];
   
    
    //dispatch_after延迟执行的方法 dispatch 只需要填写时间就可以 在里面写要执行的东西
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        for (int i = 0; i<4; i++) {
            
        //文字
          NSArray *array = @[@"123333",@"32111",@"23444",@"98000"];
        NSLog(@"%@",array);
        UILabel *label = [[UILabel alloc]init];
        label.frame = CGRectMake(100*i, 400, 100, 30);
        label.text = array[i];
            label.backgroundColor = [UIColor orangeColor];
            
            [self.view addSubview:label];
        }
    });
    
    
   
}

//图片
-(void)add:(NSString *)url
{
    NSLog(@"url-----%@",url);
    UIImageView *image = [[UIImageView alloc]init];
    image.frame = CGRectMake(0, 0, 375, 380);
    image.image = [UIImage imageNamed:url];
    [self.view addSubview:image];
    
}

//效果 点击屏幕 3秒后出现图片 四秒出现文字

线程延迟执行

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