运行循环

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的时候不会阻断消息
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容