iOS - How to slow down UISlider animation?

2019-06-01 06:09发布

问题:

I'm trying to slow down the animation of the UISlider when changing values.

So far, I've tried the old UIView animation method:

[UIView beginAnimations:@"slider" context:nil];

[UIView setAnimationDuration:5.0];
[self.slider setValue:2 animated:YES];

[UIView commitAnimations];

And the new blocks based method:

[UIView animateWithDuration:5.0 
         animations:^{
            [self.slider setValue:2 animated:YES];
                 }];

I've tried with and without the animated:YES value set. In all cases, the slider simply animates at the default speed.

Is there another tactic I should be looking at to customize the speed of the animation?

Should I subclass the slider and override anything there?

回答1:

Check out the OBSlider, a UISlider subclass with variable scrubbing speed (as seen in the iPod app on iOS) by Ole Begemann. I'm not saying it is exactly what you want but you can see how it is implemented since the code is hosted at GitHub. Basically, it subclasses UISlider and overrides the touch tracking methods:

  • beginTrackingWithTouch:withEvent:
  • continueTrackingWithTouch:withEvent:
  • endTrackingWithTouch:withEvent: