3 个互连的滑块

发布于 2024-10-08 09:40:59 字数 378 浏览 6 评论 0原文

我已经为这个问题绞尽脑汁两天了,我尝试了不同的方法,但没有一个起作用。我正在构建一个应用程序,它是一种测验。共有三个包含问题的主题。我想使用 3 个滑块来定义他们想要的每个主题的问题百分比。

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

如果我选择拥有更多历史记录,我会向上滑动历史滑块,其他滑块应根据它们必须达到 3 个滑块的 100% 的值而减少...

对算法有什么想法吗?当一个滑块达到零值时会发生什么?

数学从来都不是我的菜。

任何帮助将不胜感激。

提前致谢。

麦克风

I've been racking my brains over this problem for two days, I've tried different things but none of them work. I'm building an app which is a kind of quizz. There are three subjects which contain questions. I would like to use 3 sliders to define the percentage of questions they want on each subject.

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

If I choose to have more history, I slide the history slider up and the other sliders should decrease according to the values they have to reach 100% for the 3 sliders...

Any idea for an algorithm ? And what happens when one slider reach a zero value ?

Maths has never been my scene.

Any Help would be very much appreciated.

Thanks in advance.

Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最近可好 2024-10-15 09:40:59

虽然 Snowangelic 的答案很好,但我认为按如下方式限制不变值之间的比率更有意义。

令s1、s2、s3为滑块的当前值,因此s1+s2+s3=100。您想要求解 n1、n2、n3 滑块的新值,以便 n1+n2+n3=100。假设用户将 s1 更改为 n1。那么这会添加以下约束:

n2/n3 = s2/s3

因此,当 n1+n2+n3=100 时,问题的解决方案是

n2 = (100-n1)/(s3/s2 + 1) 或 0(如果s2=0) 且

n3 = (100-n1)/(s2/s3 + 1) 或 0(如果 s3=0)

Though Snowangelic's answer is good, I think it makes more sense to to constrain the ratio between the unchanged values as follows.

Let s1, s2, s3 be the current values of the sliders, so that s1+s2+s3=100. You want to solve for n1, n2, n3 the new values of the sliders, so that n1+n2+n3=100. Assume s1 is changed to n1 by the user. Then this adds the following constraint:

n2/n3 = s2/s3

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

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

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

卸妝后依然美 2024-10-15 09:40:59

在 33.333...% 处启动所有三个滑块

当用户将滑块向上移动 10% 时:将其他两个滑块向下移动 5%。但如果两个滑块之一达到 0 =>只动另外百分之十。所以它给出了这样的内容:

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

另一种可能性是考虑可用资源的总和为 100,资源被分成 n 个桶(在您的情况下为 3)。当用户移动滑块时,他固定了相应桶中的资源数量。因此,您可以从其他存储桶中获取资源,也可以将资源放入其他存储桶中。

您有类似的情况:

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 --;
    }
  }
}

假设修改是正数(修改后的存储桶中的新数字比以前更高)。这个小算法适用于任意数量的存储桶(在您的情况下是滑块)。

Start all three sliders at 33.333...%

When the users moves a slider up say 10% : move the two other sliders down of 5%. But if one of two slider reaches 0 => only move the other one of ten percent. So 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 os the available ressources is 100, the ressources are separated into n buckets (in your case 3). When the user moves a slider, he fixes the number of ressources in the corresponding bucket. And so you may either take ressources from other bucket or put ressources 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 --;
    }
  }
}

That is assuming the modification is positive (new number in the modified bucket is higher than before). This small algorithm would work with any number of buckets (sliders in your case).

束缚m 2024-10-15 09:40:59

应该审查以下算法,当然还要对其进行优化。这只是我组装的东西,我还没有测试过。

使用最大值和最小值初始化每个滑块,并根据需要设置初始值,但要遵守 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];

将三个滑块设置为相同的选择器:

[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];

选择器应该执行类似的操作:

- (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];
}

如果此代码有问题,请告诉我,我可以修复它!

The following algorithm should be reviewed and of course optimized. It is only something that I have put together and I've not tested it.

initialize each slider with a max and minimum value and set the inital value as desired, but respecting that 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 slider 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 that:

- (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 there is something wrong with this code, please let me know, and I can fix it!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文