Search code examples
iphoneobjective-cioscocoa-touchuislider

slider increment automatically in time period


I am trying to use the current time in my movie player to increment a sliders value in my iPad app.

slider.maximumValue=10;
slider.minimumValue=0;

How can I increment slider.value from 0 to 10 in a specific time period?


Solution

  • You can use an NSTimer to repeatedly add 1 to your slider value if you know the specific time period beforehand:

    NSTimer *aTimer = [NSTimer scheduledTimerWithTimeInterval:(timePeriod/slider.maximumValue)
    target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
    
    - (void)timerFired:(NSTimer*)theTimer {
        [slider setValue:slider.value + 1.0];
        if(slider.value == slider.maximumValue) {
            [theTimer invalidate];
        }
    }