UISlider Help.

Discussion in 'iOS Development' started by cocotutch, Jul 9, 2009.

  1. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I have a slider, which I need to control the Ball size (radius) inside my app. It works to a degree but everytime I go to use it, it automatically applies itself to 1.0 or 0.0, it never stays in between at my values.

    Here is my code:

    Code:
    if(slider.value = 1.0)
    	{
    		ball.radius = 80.0;
    	}
    	else if(slider.value = 0.9)
    	{
    		ball.radius = 70.0;
    	}
    	else if(slider.value = 0.8)
    	{
    		ball.radius = 60.0;
    	}
    	else if(slider.value = 0.7)
    	{
    		ball.radius = 50.0;
    	}
    	else if(slider.value = 0.6)
    	{
    		ball.radius = 40.0;
    	}
    	else if(slider.value = 0.5)
    	{
    		ball.radius = 30.0;
    	}
    	else if(slider.value = 0.4)
    	{
    		ball.radius = 20.0;
    	}
    	else if(slider.value = 0.3)
    	{
    		ball.radius = 10.0;
    	}
    	else if(slider.value = 0.2)
    	{
    		ball.radius = 10.0;
    	}
    	else if(slider.value = 0.1)
    	{
    		ball.radius = 10.0;
    		
    	}	
    I am probably doing something stupid, but please help.

    cocotutch
  2. bddckr

    bddckr Active Member

    Joined:
    Dec 2, 2007
    Messages:
    1,434
    Likes Received:
    18
    Device:
    iPhone 4 (Black)
    Use == instead of =. You are comparing, not assigning!

    Also I would try to make the if statements not that ugly

    Please Register or Log in to view images



    Code:
    if (slider.value > 0.2) {
    	ball.radius = slider.value * 10.0 - 20.0;
    } else if (slider.value == 0.2) {
    	ball.radius = 10.0;
    } else if (slider.value == 0.1) {
    	ball.radius = 10.0;
    }
    
  3. NolesFans

    NolesFans New Member

    Joined:
    Sep 23, 2008
    Messages:
    404
    Likes Received:
    0
    Device:
    iPod touch
    even you can uses || as or.

    like this

    Code:
    if (slider.value > 0.2) {
    	ball.radius = slider.value * 10.0 - 20.0;
    } else if (slider.value == 0.2 || slider.value == 0.1) {
    	ball.radius = 10.0;
    }
    
  4. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    Use switch statements.

    Code:
    switch(object.integervalue) {
            case someinteger: 
                    // do stuff
                    break;
    }
  5. jbonedev

    jbonedev New Member

    Joined:
    May 24, 2009
    Messages:
    44
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Or since each case is just assigning values to the same variable, use a map to fetch the value.
  6. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    Thank-you for all your help everybody, however I solved it in one line:

    Code:
    Ball.radius = slider.value;
    I just set the maximum value of the slider to 100.0 in Interface Builder, allowing me to easily modify it in the Future in my App Updates on the Store.

    cocotutch
  7. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Good job on setting the slider values. That is one of the most overlooked properties of any UIKit object.

    One alternate option would have been for you to just multiply slider.value by 100. Or set your image and preformed a scaling transform to the slider.value. Scaling something by 1 equals full sized while .5 is half sized, etc.


    [Edit: I would have chosen the latter of the two alternatives I presented.]
  8. cocotutch

    cocotutch Community Development Team Staff Member

    Joined:
    Oct 6, 2008
    Messages:
    1,285
    Likes Received:
    117
    Device:
    5G iPod touch
    I have rendered the Ball in CG, so setting the Radius is the only way I can do it (I think)

    Please Register or Log in to view images

    But those tips are useful, I experimented with them in a test application and they worked wonders.

    Please Register or Log in to view images



    Maybe we could add this to a UIKit Database for people who need help with UIKit Elements in their projects, or do we have one of those already?

    cocotutch
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You can get an image from your CGContext pretty easily. That might be worth doing, for the sake of moving it, resizing it, etc.

    Also, if you're drawig more than a single solid colored circle, try to use CGgradients instead of CGShading. CGShading leaks.

Share This Page