Fun Headache: 3 UISliders - Total Value is always 1.0
I am trying to accomplish something, which leads me into a circle.
I have 3 UISliders.
control1, 2 and 3. their min value is 0. max is 1.
What I want to do is if a user change slider 1 value, slider 2 and 3 is automatically changed and the total will remain 1.0.
For eg, if slider 1 is slided to 0.5, then perhaps, slider 2 and 3 becomes 0.25 each (so 0.5+0.25+0.25 = 1). I don't really care what are values of slider 2 and 3, as long as the total makes up 1.
You know, of course, we are not allowed to do your homework.
Quote:
Originally Posted by rocotilos
I am trying to accomplish something, which leads me into a circle.
I have 3 UISliders.
control1, 2 and 3. their min value is 0. max is 1.
What I want to do is if a user change slider 1 value, slider 2 and 3 is automatically changed and the total will remain 1.0.
For eg, if slider 1 is slided to 0.5, then perhaps, slider 2 and 3 becomes 0.25 each (so 0.5+0.25+0.25 = 1). I don't really care what are values of slider 2 and 3, as long as the total makes up 1.
I am trying to accomplish something, which leads me into a circle.
I have 3 UISliders.
control1, 2 and 3. their min value is 0. max is 1.
What I want to do is if a user change slider 1 value, slider 2 and 3 is automatically changed and the total will remain 1.0.
For eg, if slider 1 is slided to 0.5, then perhaps, slider 2 and 3 becomes 0.25 each (so 0.5+0.25+0.25 = 1). I don't really care what are values of slider 2 and 3, as long as the total makes up 1.
Go on then, solve the problem for me with codes.
I'm not going to write the code for you, and neither should anybody else.
The problem is simple.
Write an IBAction sliderChanged. Attach it to the value changed event for all 3 sliders. Put tags on all 3 sliders.
When the user changes one of the sliders, sum all the values. If the value is now >1, subtract half the "overflow" from the other 2 sliders.
If the value is now less than 1, add half the "deficit" to the other 2 sliders.
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
yeah, I know nobody would be writing codes for people. I was hoping
there was someone like me out there. I like to give out codes if I can.
Anyway, here's what I came up with... it works.
Tie this code up to a slider. do the same for the other 2 sliders.
Probably there are easier way to do it, and maybe my code sucks, but
there u go.
V1,V2 and V3 are global CGFloats
Code:
V1 = control1.value;
CGFloat tot = V1 + V2 + V3;
CGFloat amt; // amt to offset
if (tot<1.0) { // value is reducing
amt = 1.0 - tot;
if ((V2<1.0)&&(V3<1.0)) {
V2 += amt/2.0;
V3 += amt/2.0;
}
if ((V2>=1.0)&&(V3<1.0)) {
V2 += 0.0;
V3 += amt;
}
if ((V2<1.0)&&(V3>=1.0)) {
V2 += amt;
V3 += 0.0;
}
} else {
amt = tot - 1.0;
if ((V2>0.0)&&(VGREYSCALE3>0.0)) {
V2 -= amt/2.0;
V3 -= amt/2.0;
}
if ((V2<=0.0)&&(V3>0.0)) {
V2 -= 0.0;
V3 -= amt;
}
if ((V2>0.0)&&(VGREYSCALE3<=0.0)) {
V2 -= amt;
V3 -= 0.0;
}
}
[control2 setValue:V2 animated:YES];
[control3 setValue:V3 animated:YES];
The problem with giving code is that it doesn't help anyone in the long run. The person receiving the code probably won't know how it works and won't be able to solve a similar problem down the line.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
The problem with giving code is that it doesn't help anyone in the long run. The person receiving the code probably won't know how it works and won't be able to solve a similar problem down the line.
I beg to differ. I have used lots of codes in here and I do understand them, but sometimes haven't thought of writing the code in that particular way. And I gain my experience in coding half of it is thru this method. The coding technique is also learnt and re-use whenever is suitable in my app as well.
By not giving code, I don't mean not giving any hints whatsoever of what the code should like. Usually, I'll describe what the code should do with some method names tossed in. This prevents people from just copying and pasting and moving on. And sometimes, I do post some code snippets because it's something that's obscure that when you see it, you'll know or it's just so dead drop simple, that there really is nothing to think about, you just have to know it.
Also, the people asking the questions need to do some work. If people just want code blocks, let them hire a developer.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
By not giving code, I don't mean not giving any hints whatsoever of what the code should like. Usually, I'll describe what the code should do with some method names tossed in. This prevents people from just copying and pasting and moving on. And sometimes, I do post some code snippets because it's something that's obscure that when you see it, you'll know or it's just so dead drop simple, that there really is nothing to think about, you just have to know it.
Also, the people asking the questions need to do some work. If you just want code blocks, hire a developer.
Wow. I am not forcing ANYONE to post codes for me. I put up a question, and those who want to give a code, do so, and those who do not want to, can choose to ignore my thread.
Sometimes, I just take a break from my own app coding by writing snippets for others who asks around here. See my history of posts, and you will know this. But I don't know why you're seem to be bothered by these act of giving though. It's not like I come here and say "write me codes, or I will shoot you".
"This prevents people from just copying and pasting and moving on"
And what is wrong with that? I feel there is no need to reinvent the wheel. For example, we all use those Frameworks in iOS. That is also basically a "copy and paste" act as well. But on a different scale of course.
Sorry, by "you" I just mean people that come here in general. I think this the second time I pissed someone off with this mistake.
The difference is that those frameworks aren't built using very basic things if you know what I mean. Frameworks are complex, at least most of the components in them are. A screen that has a date picker and a label that reflects the date picker is not. If I just give code to do that, the person will never learn how to solve the problem themselves. They won't learn how to use a NSDateFormatter or how to use target actions. I understand if people don't want to learn the complex stuff like an RSS reader (it's not the most complicated thing but to a beginner, it might look scary) but they need to know the basics. And in your case, I do believe that if I gave you some code, you'd probably take some time to analyze it and see it how works. If everyone did that, I'd have no problem writing short code snippets. Other people won't do that though, they'll straight copy and paste it and never look back until a user complains about a bug.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
Sorry, by "you" I just mean people that come here in general. I think this the second time I pissed someone off with this mistake.
The difference is that those frameworks aren't built using very basic things if you know what I mean. Frameworks are complex, at least most of the components in them are. A screen that has a date picker and a label that reflects the date picker is not. If I just give code to do that, the person will never learn how to solve the problem themselves. They won't learn how to use a NSDateFormatter or how to use target actions. I understand if people don't want to learn the complex stuff like an RSS reader (it's not the most complicated thing but to a beginner, it might look scary) but they need to know the basics. And in your case, I do believe that if I gave you some code, you'd probably take some time to analyze it and see it how works. If everyone did that, I'd have no problem writing short code snippets. Other people won't do that though, they'll straight copy and paste it and never look back until a user complains about a bug.
That's where theory of relativity comes in. For you and many other experts, it is very basic things. But for newbies especially, it is not that basic. Sometimes newbies just need that few lines of codes to get started and to make sense of coding. This is my stance and why I chose to give out codes, because I've been there myself.
I do agree though, there are quite a handful people who just plain copy things, without wanting to learn. But, they'll get what's coming to their apps if that's what they do, sooner or later (user complains bug, app crashes, and so on).
I am not pissed or anything, just that, I can understand your stance on this, and I accept that, so I just wish you can accept my stance on giving out codes.
That would just always set the value of the two other sliders to half the adjusted slider, without any regard to what their previous values were. So it'd be impossible to set values like slider1 = 0.8 slider2 = 0.15 slider3 = 0.05.
Duncan C already gave the "right" general solution. The only adjustment I might make is to store the slider references in an array and iterate over it in the sliderChanged -callback. So a bit like this:
Code:
- (id) init {
// normal init things here
// add this to keep a handy reference to all your sliders
slidersArray = [NSArray arrayWithObjects: slider1, slider2, slider3, ..., nil];
}
- (IBAction) valueChanged:(UISlider *)slider {
float total;
float change;
UISlider *aSlider;
for (aSlider in slidersArray)
total += aSlider.value;
change = (total-1)/[slidersArray count];
for (aSlider in slidersArray) {
if (aSlider != slider)
[aSlider setValue:aSlider.value + change animated:YES];
}
}
That code is more complicated than it needs to be for this case, but it should work for any number of sliders. So if you decide to add or remove a slider later on, you just need to change one line in the init method and it should still work. You could do the same without the slidersArray if you tag the sliders properly and then iterate over all controls and find the sliders using the tag(s), but I won't go into that. It's a slightly different solution and it's a matter of taste if you want to use IB and tags or hand code. The IB+tags is the more flexible solution, but carries a bit more overhead.
BOOM! That's what I'm talking about, fiftysixty. Thanks, I'll have a look at your code later.
Be warned that I didn't check any of that code, so there probably are some errors there. For example when calculating change, you maybe should cast the integers to floats.