3 interlocking sliders

I’ve been racking my brains over this problem for two days, I tried different things, but none of them work. I am creating an application that is a quiz. There are three topics that contain questions. I would like to use 3 sliders to determine the percentage of questions they want for each question.

ex : slider one = History slider two = Maths slider three = Grammar 

If I prefer to have more history, I move the history slider up, and the other sliders should decrease according to the values ​​that they should reach 100% for the three sliders ...

Any idea for an algorithm? And what happens when one slider reaches zero?

Math has never been my scene.

Any help would be greatly appreciated.

Thanks in advance.

Mike

+4
source share
3 answers

The following algorithm should be reviewed and, of course, optimized. This is just what I put together, and I have not tested it.

initialize each slider with the maximum and minimum values ​​and set the desired value, but observing this x + y + z = 1 .

 [self.slider1 setMinimumValue:0.0]; [self.slider1 setMaximumValue:1.0]; [self.slider1 setValue:0.20]; [self.slider2 setMinimumValue:0.0]; [self.slider2 setMaximumValue:1.0]; [self.slider2 setValue:0.30]; [self.slider3 setMinimumValue:0.0]; [self.slider3 setMaximumValue:1.0]; [self.slider3 setValue:0.50]; 

Set the three sliders to the same selector:

 [self.slider1 addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; [self.slider2 addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; [self.slider3 addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged]; 

The selector should do something like this:

 - (void)valueChanged:(UISlider *)slider { UISlider *sliderX = nil; UISlider *sliderY = nil; UISlider *sliderZ = nil; if (slider == self.slider1) { sliderX = self.slider1; sliderY = self.slider2; sliderZ = self.slider3; } else if (slider == self.slider2) { sliderY = self.slider1; sliderX = self.slider2; sliderZ = self.slider3; } else { sliderY = self.slider1; sliderZ = self.slider2; sliderX = self.slider3; } float x = sliderX.value; float y = sliderY.value; float z = sliderZ.value; // x + y + z = 1 // Get the amout x has changed float oldX = 1 - y - z; float difference = x - oldX; float newY = y - difference / 2; float newZ = z - difference / 2; if (newY < 0) { newZ += y + newY; newY = 0; } if (newZ < 0) { newY += z + newZ; newZ = 0; } [sliderY setValue:newY animated:YES]; [sliderZ setValue:newZ animated:YES]; } 

If something is wrong with this code, let me know and I can fix it!

0
source

Although the answer to Snowangelic is good, I think it makes sense to limit the relationship between immutable values ​​as follows.

Let s1, s2, s3 be the current values ​​of the sliders, so s1 + s2 + s3 = 100. You want to solve for the n1, n2, n3 new values ​​of the sliders, so n1 + n2 + n3 = 100. Suppose that s1 is changed to n1 user. This then adds the following restriction:

n2 / n3 = s2 / s3

So, the solution to the problem with n1 + n2 + n3 = 100,

n2 = (100-n1) / (s3 / s2 + 1) or 0 (if s2 = 0) and

n3 = (100-n1) / (s2 / s3 + 1) or 0 (if s3 = 0)

+6
source

Run all three sliders on 33.333 ...%

When users move the slider, say 10%: move the other two sliders by 5%. But if one of the two sliders reaches 0 =>, move only one out of ten percent. Therefore, it gives something like this:

 User moved slider of x (my be positive or negative) for first slider if slider -x/2 > 0 and x/2 < 100 move this slider of -x/2 else move the other slider of -x/2 for second slider if slider -x/2 > 0 and x/2 < 100 move this slider of -x/2 else move the other slider of -x/2 end 

Another possibility would be to consider that the sum of available resources is 100, the resources will be divided into n buckets (in your case 3). When the user moves the slider, he fixes the amount of resources in the corresponding bucket. Thus, you can either take ressources from another bucket or put resources in these other buckets.

You have something like:

 state 1 ; modified bucket ; new number of ressources in that bucket modification = new number of ressources in the bucket - number of rescources in the state 1 for (int i=0 ; modification > 0 ; i++){ i=i%nbr of buckets; if(bucket i != modified bucket){ if(number of ressources in bucket i-- > 0 && number of ressources in bucket i-- < 100){ number of ressources in bucket i--; modification --; } } } 

It is assumed that the modification is positive (the new number in the modified bucket is higher than before). This small algorithm will work with any number of buckets (sliders in your case).

+1
source

Source: https://habr.com/ru/post/1332973/


All Articles