iOS timer 定时器

定时器1(锁屏状态没问题)

    NSRunLoop *currentRunloop = [NSRunLoop currentRunLoop];
    
    //该方法内部自动把  timer 添加到runloop中,并且设置运行模式为默认
    //在主线程和子线程情况不一样(
//    每条线程都有唯一的一个与之对应的RunLoop对象
//    )
    [NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    //开启runloop
    [currentRunloop run];
    


定时器2(锁屏状态有问题)


  
    NSTimer *timer =  [NSTimer timerWithTimeInterval:10.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    //2.添加定时器到runLoop中,指定runloop的运行模式为NSDefaultRunLoopMode
    /*
     第一个参数:定时器
     第二个参数:runloop的运行模式
     */
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];


-(void)run
{
    
    
    
    
}

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

推荐阅读更多精彩内容