UIView animateWithDuration not working with UISlider

495 views Asked by At

I want the slider to set value slowly and for that I am using this code

    [UIView animateWithDuration:2.0 animations:^{
         [self.slider setValue:(float)val];
    }];

But this is not working. Please help me out.

2

There are 2 answers

0
Akki On BEST ANSWER

Use [self.slider setValue:(float)val animated:YES]; instead.

0
AudioBubble On

What I have found is that the animation will not work is the animation is scheduled while the view is still loading. So instead of this (which does not animate):

-(void)animateSlider: duration:(NSTimeInterval)duration
{
    _slider.maximumValue = duration;
    _slider.minimumValue = 0;
    [UIView animateWithDuration:duration
                        delay:0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [_slider setValue:duration animated:YES];
                     }
                     completion:^(BOOL finished){}
    ];
}

I do the following, where animateSlider is an internal method:

-(void)animateSlider
{
    float value = _slider.maximumValue;
    [UIView animateWithDuration:value
                        delay:0
                        options: UIViewAnimationOptionCurveLinear
                     animations:^{
                         [_slider setValue:value animated:YES];
                     }
                     completion:^(BOOL finished){}
    ];
}

-(void)animateSlider: duration:(NSTimeInterval)duration
{
    _slider.maximumValue = duration;
    _slider.minimumValue = 0;
     [self performSelector:@selector(animateSlider) withObject:nil afterDelay:0];
}

The afterDelay:0 means that the animation will be scheduled for the very next runloop.