Search code examples
iosobjective-cuislider

sliderValueDidChange getting called multiple times


I have a UISlider and I'd like to slide 3 UILabels below it along with the slider, as well as update their values, which are dependent on the slider value. When dragging the slider, the location flashes back and forth between where it should be and back to the center position. Inserting a breakpoint, for a very minute amount, the sliderValueDidChange method gets called multiple times and the sliderValue shows different values. Any ideas?

- (IBAction)sliderValueDidChange:(UISlider *)sender {
// Update the labels
float sliderValue = sender.value;
percentSplitLabel.text = [NSString stringWithFormat:@"%.0f%@", sliderValue * 100, @"%"];
themSplitValueLabel.text = [NSString stringWithFormat:@"%@%.2f", @"$", totalToBeAddedToTabValue];
meSplitValueLabel.text = [NSString stringWithFormat:@"%@%.2f", @"$", totalBillValue - totalToBeAddedToTabValue];

// Reposition the numbers below
CGRect sliderFrame = splitBillSlider.frame;
percentSplitLabel.center = CGPointMake(sliderFrame.origin.x - sliderFrame.size.width/2.0 + sliderFrame.size.width * sliderValue + 150, percentSplitLabel.center.y);
themSplitValueLabel.center = CGPointMake(percentSplitLabel.center.x + percentSplitLabel.frame.size.width + 10, percentSplitLabel.center.y);
meSplitValueLabel.center = CGPointMake(percentSplitLabel.center.x - percentSplitLabel.frame.size.width, percentSplitLabel.center.y);

Solution

  • If you're using Auto Layout in your views try to disable it (File inspector > Uncheck “Enable Autolayout”).