NSTimer
1.在子线程添加NSTimer
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(next) userInfo:nil repeats:YES]; NSLog(@"主线1"); dispatch_async(dispatch_queue_create("123", DISPATCH_QUEUE_SERIAL), ^{ NSLog(@"分1"); [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; //注意:一定要加这句,不然不会进行循环 [[NSRunLoop currentRunLoop] run]; //这个不会运行,[[NSRunLoop currentRunLoop] run]后面的代码读不会执行 NSLog(@"分2"); }); NSLog(@"主线2"); - (void)next { dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"---"); }); }
2.或者NSRunLoopCommonModes,这样在滑动的时候定时器照样运行
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];