Search code examples
iphoneios6uislider

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?


Solution

  • 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: