Search code examples
iosobjective-ccocoa-touchuislider

Update slider value on label when slider animates


I am using slider in my app. I have done all the setup only problem i am facing is the label on which i want slider values is not updating. Have a look at my code. I have a button which on click animates the slider. I am not able to update my label as it shows 0.0 throughout the animation. Any help will be highly appreciated.

- (IBAction)btnClicked:(id)sender
 {
[UIView animateWithDuration:10 animations:^{
        [self.slider setValue:0.0 animated:true];
     } completion:^(BOOL finished) {
        [UIView animateWithDuration:10 animations:^{
            [self.slider setValue:100.0 animated:true];
        }];
    }];
 }

Solution

  • Your code is not working because you try to set the maximum value to the slider so that it is simply set that maximum value with animation nothing more. Try my code that may be help you.

    Change your btnClicked with this

     - (IBAction)btnClicked:(id)sender {
          [self.slider setValue:0 animated:true];
          [self setSliderValue:1];
     }
    

    Add this setSliderValue method

    - (void)setSliderValue:(float)value {
        [UIView animateWithDuration:0.2 animations:^{
        [self.slider setValue:value animated:true];
        } completion:^(BOOL finished) {
            self.lblSliderValue.text = [NSString stringWithFormat:@"%0.0f", self.slider.value];
            if (self.slider.value != self.slider.maximumValue) {
                [self setSliderValue:value+1];
            }
        }];
    }
    

    Hope this will help you.