问题描述
我已经绞尽脑汁解决这个问题两天了,我尝试了不同的东西,但没有一个能奏效。我正在构建一个应用程序,这是一种quizz。有三个主题包含问题。我想使用3个滑块来定义每个主题所需的问题百分比。
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
如果我选择了更多历史记录,我向上滑动历史记录滑块,其他滑块应根据3个滑块的值达到100%而减少...
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.
任何帮助非常感谢。
提前致谢。
Mike
推荐答案
应该检查以下算法并进行优化。这只是我放在一起的东西,我没有测试过。
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.
用最大值和最小值初始化每个滑块并根据需要设置初始值,但是尊重 x + y + z = 1
。
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!
这篇关于3个相互连接的滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!