bmende
bmende

Reputation: 741

How to programmatically move a slider at a set duration using IOS 6?

I have an iPhone application with a UISlider. When my user presses the play button, I want my slider to move from a value of 1, slowly, to a value of 100. The slider slides for the duration of the recording that gets played back when my user presses the play button.

I looked at the UISlider documentation. The only method that seems like it might help me is the setValue:animated: method. However, when animated=yes, the slider moves to value 100 way too fast.

Any way to slow it down a little?

Upvotes: 1

Views: 3971

Answers (2)

Abizern
Abizern

Reputation: 150675

You can let the UIView animate itself rather than working out the tweening yourself, or writing callbacks. For Example, this animates the slider to it's maximum value from it's current value in 5 seconds. And it's a lot smoother than trying to do it yourself.

- (IBAction)play:(id)sender {
    [sender setEnabled:NO];
    [UIView animateWithDuration:5.0 animations:^{
        self.slider.value = self.slider.maximumValue;
    }];
}

If you don't believe me - here's a sample project that you can download to see it for yourself:

Upvotes: 5

woz
woz

Reputation: 11004

You should use scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: which is part of NSTimer to increment the slider more slowly. Increase the position of the slider on every tick. Something like this:

NSTimer *timerForSlider = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateSlider) userInfo:nil repeats:YES];

-(void)updateSlider {
    [slider setValue:slider.value+1];
    if(slider.value == MAXIMUM_VALUE) {
        [timerForSlider invalidate];
    }
}

Upvotes: 0

Related Questions