1. 程式人生 > >IOS學習(二十二)runloop與timer

IOS學習(二十二)runloop與timer

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //方式1
    //自動加在runloop下,執行
    //這種方式下,如果滑動動textview,timer會暫停,即run方法不會被執行
    [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    
    //方式2
    //同方式1
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(run) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    
    //方式3
    //這種方式,只有textview在滑動的時候,timer才會執行
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
    
    //方式4
    //NSRunLoopCommonModes佔位標記,標記NSDefaultRunLoopMode+UITrackingRunLoopMode
    //即不管textview是否在滑動,timer都會執行
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}

- (void)run{
    NSLog(@"---run---");
}