Change the time interval of a Timer

后端 未结 2 1371
深忆病人
深忆病人 2021-01-24 23:04

here is my question: Is it possible to increase the scheduledTimerWithTimeInterval:2 for example of \"3\" after 10 seconds in ViewDidLoad for example. E.g., from th

2条回答
  •  臣服心动
    2021-01-24 23:42

    Reschedule the timer recursively like this:

    float gap = 0.50;
    
    [NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
    
    -(void) onTimer {
        gap = gap + .05;
        [NSTimer scheduledTimerWithTimeInterval:gap target:self selector:@selector(onTimer) userInfo:nil repeats:NO];
    }
    

    ========

    Or according to How can I update my NSTimer as I change the value of the time interval

    Invalidate it with:

    [myTimer invalidate];
    

    Then create a new one with the new time. You may have to set it to nil first as well.

    myTimer = nil;
    myTimer = [NSTimer scheduledTimerWithTimeInterval:mySlider.value 
                                               target:self 
                                             selector:@selector(myMethod) 
                                             userInfo:nil 
                                              repeats:YES];
    

提交回复
热议问题