Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 11-16-2011, 02:10 AM   #1 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Talking 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.

Go on then, solve the problem for me with codes.
rocotilos is offline   Reply With Quote
Old 11-16-2011, 07:01 AM   #2 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 92
fbronner is on a distinguished road
Default

You know, of course, we are not allowed to do your homework.

Quote:
Originally Posted by rocotilos View Post
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.
fbronner is offline   Reply With Quote
Old 11-16-2011, 07:38 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by rocotilos View Post
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.

It would take about 5 lines of code, maybe less.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


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.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 11-20-2011, 02:22 AM   #4 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

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

Last edited by rocotilos; 11-20-2011 at 02:33 AM.
rocotilos is offline   Reply With Quote
Old 11-20-2011, 12:02 PM   #5 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

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.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 11-20-2011, 08:37 PM   #6 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
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.
rocotilos is offline   Reply With Quote
Old 11-20-2011, 09:37 PM   #7 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

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.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.

Last edited by Domele; 11-21-2011 at 01:38 PM.
Domele is offline   Reply With Quote
Old 11-21-2011, 08:20 AM   #8 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
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.

Last edited by rocotilos; 11-21-2011 at 08:24 AM.
rocotilos is offline   Reply With Quote
Old 11-21-2011, 01:31 PM   #9 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

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.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.

Last edited by Domele; 11-21-2011 at 01:36 PM.
Domele is offline   Reply With Quote
Old 11-21-2011, 02:04 PM   #10 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Seattle, WA
Posts: 408
RickSDK is on a distinguished road
Default

why not just do this:

float x=slider1;
float remainder = 1-x;
slider2 = remainder/2;
slider3 = remainder/2;
RickSDK is offline   Reply With Quote
Old 11-21-2011, 08:47 PM   #11 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

Quote:
Originally Posted by Domele View Post
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.

Cheers mate.
rocotilos is offline   Reply With Quote
Old 11-21-2011, 08:48 PM   #12 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

Quote:
Originally Posted by RickSDK View Post
why not just do this:

float x=slider1;
float remainder = 1-x;
slider2 = remainder/2;
slider3 = remainder/2;
Thanks for your help, but I already solved it myself see my post up there with the codes.
rocotilos is offline   Reply With Quote
Old 11-22-2011, 04:56 AM   #13 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Quote:
Originally Posted by RickSDK View Post
why not just do this:

float x=slider1;
float remainder = 1-x;
slider2 = remainder/2;
slider3 = remainder/2;
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.
fiftysixty is offline   Reply With Quote
Old 11-22-2011, 08:16 PM   #14 (permalink)
almostfunnydev
iPhone Dev SDK Supporter
 
rocotilos's Avatar
 
Join Date: Oct 2009
Age: 34
Posts: 3,015
rocotilos is on a distinguished road
Default

BOOM! That's what I'm talking about, fiftysixty. Thanks, I'll have a look at your code later.
rocotilos is offline   Reply With Quote
Old 11-23-2011, 05:17 AM   #15 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Quote:
Originally Posted by rocotilos View Post
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.
fiftysixty is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 393
17 members and 376 guests
7twenty7, Alex-alex, Apptronics RBC, baja_yu, chiataytuday, dre, gwelmarten, ipodphone, jeroenkeij, jleannex55, matador1978, mbadegree, n00b, pbart, QuantumDoja, Retouchable, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,125
Posts: 402,910
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:24 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0