0
votes

I am using a UISlider in my app where I add max and min image for slider. Every thing works good but look at the image below. When I click on button the animation starts and values on label change but the animation on slider directly jumps to 6 which you can see it and the same thing happens when the slider reaches it max value(100) from 96 the slider min image(orange color) disappears and the label continues to show values 97..98..99..100.enter image description here

Here's the code snippet for reference :

 - (void)viewDidLoad
 {
  [super viewDidLoad];
  UIImage *sliderMinimum = [[UIImage imageNamed:@"slider_max"]    stretchableImageWithLeftCapWidth:14 topCapHeight:0];
  [self.slider setMinimumTrackImage:sliderMinimum forState:UIControlStateNormal];
  UIImage *sliderMaximum = [[UIImage imageNamed:@"slider_min"] stretchableImageWithLeftCapWidth:14 topCapHeight:0];
  [self.slider setMaximumTrackImage:sliderMaximum forState:UIControlStateNormal];
 }

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

- (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];
    }
}];
}
1
You need to force a refresh of the UI component... setNeedsDisplayDominic
@Dominic how do i set it and where ! Can you elaborate your solution plziDeveloper
put this after you set the text... [self.lblSliderValue setNeedsDisplay]Dominic
I would also recommend wrapping your [self setSliderValue:value+1]; line inside: dispatch_async(dispatch_get_main_queue(), ^{ your code goes here }); to delay the next animation until the after the current run loop has completed.Dominic
@Dominic let me clear! The label is showing perfect values as animation starts i.e. 1-100 my prob is the slider animation that directly jumps to 6. You can see the gif image in my questioniDeveloper

1 Answers

0
votes

Try with this:

[slider addTarget:self
  action:@selector(roundValue:) 
  forControlEvents:UIControlEventValueChanged];

And in the roundValue function set the value:

- (void)roundValue{

    [slider setValue:((int)((slider.value + 0) / 1) * 1) animated:NO];

   }