UISlider maximum values adjustments

2019-05-25 21:29发布

问题:

I am having three sliders and these shows the percentages for different elements what i need the max value of all is 100 (a+b+c=100).

Now these all are dependent to each other and helps in making a pie chart.

currently all can set to max value that is 100.

how can make this dependency and max value logically.

Thanx in advance.

回答1:

In methods where you handle your sliders changes adjust maximum values for all sliders depending on current ones, e.g.:

- (void) sliderChanged:(UISlider*)sender{
    slider1.maximumValue = 100 - slider2.value - slider3.value;
    // etc
}

Edit: Adjusting maximum value may indeed lead to not behaviour. Instead to ensure that the sum does not exceed 100 you can manually adjust slider's value if it is more than desired:

- (void) sliderChanged:(UISlider*)sender{
    //sender is slider1
    sender.value = MIN(sender.value, 100 - slider2.value - slider3.value);
    // etc
}