NSRunloop

2018-03-08  本文已影响24人  找个地方记录点东西

NSRunloop mode中source

source0代表用户交互事件

source1 代表系统内核处理事件

    //监听runloop mode中obersver状态;

   CFRunLoopObserverRef obersver = CFRunLoopObserverCreateWithHandler(CFAllocatorGetDefault(),kCFRunLoopAllActivities,YES,0,^(CFRunLoopObserverRef observer,CFRunLoopActivity activity){

   });

    CFRunLoopAddObserver(CFRunLoopGetCurrent(),obersver,kCFRunLoopDefaultMode);

    CFRelease(obersver);

NSRunloop mode中timer

    /****  NSTimer计时器  ****/

    //不用添加到runloop直接调用

    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(test)userInfo:nil repeats:YES];

    /*

        需要添加至runloop中,

        NSDefaultRunLoopMode默认模式默认两秒调用,当界面拖转时,会移除当前模式,不再调用

        UITrackingRunLoopMode拖拽模式,当界面拖拽时进入拖转模式。调用方法

        NSRunLoopCommonModes间隔模式一直调用

     */

    NSTimer *timer =[NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test)userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];

    /****  GCD计时器  ****/

    //GCD定时器比较准。

    dispatch_queue_t queue = dispatch_get_global_queue(0,0);

    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0,0,queue);

    dispatch_source_set_timer(timer,DISPATCH_TIME_NOW,2.0 * NSEC_PER_SEC,0 * NSEC_PER_SEC);

    dispatch_source_set_event_handler(timer,^{

        NSLog(@"调用的方法");

    });

    dispatch_resume(timer);

上一篇 下一篇

猜你喜欢

热点阅读