运行循环

iOS运行循环

NSTimer使用

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(hello:) userInfo:@"hello world!" repeats:YES];
-(void)hello:(NSTimer *)timer
{
    NSLog(@"%@", timer.userInfo);
}

停止计时器

//执行完之后,计时器销毁,无法再继续使用
[timer invalidate];

可重用计时器

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(hello:) userInfo:@"hello world" repeats:YES];

// 加入运行时中
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
//NSRunLoopCommonModes -> scroller的时候不会阻断消息

你可能感兴趣的:(运行循环)