UIStepper value to image.alpha

Discussion in 'iOS Programming' started by larswik, Aug 24, 2012.

  1. larswik macrumors 68000

    Sep 8, 2006
    I have a ViewController where I set up values for main page which is a different view. These settings are saved into an NSDictionary and then to a plist. One of these settings is a UIStepper and on the main ViewController this value controls the alpha setting on a UIImageView.

    A stepper value returns a double. The range of an alpha for a UIImageView is 0.0 to 1.0 so I need to convert.

    I save the code from my VC1 to the NSDict and to plist.
    mainImageAlpha = [NSString stringWithFormat:@"%f",[[NSNumber numberWithDouble:[(UIStepper *) sender value]]floatValue]];
    Then in VC2 after I read in the plist to an NSDict

    backGroundWhiteWash.alpha = [[characterLoadInfo objectForKey:@"mainImageAlpha"]floatValue];
    If I set the stepper to a value of 6. The final float value is 6.0 when I need 0.6. I am at a loss as to how to best convert this to have a value from 0.0 to 1.0? I thought well I'll just flip the numbers to so 6.0 would be 0.6 but that won't work for 1.0 alpha value.

    Can some one push me in the right direction to convert this?

  2. Reason077 macrumors 68020


    Aug 14, 2007
    Dividing the stepper.value by stepper.maximumValue will give you a result between 0.0 and 1.0.
  3. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia

    You can make a stepper use whatever range of values you want. It has properties minimumValue, maximumValue, and stepValue. You could set minimumValue to 0, maximumValue to 1.0, and stepValue to .1, or .05, or whatever. Then you could assign the stepper value directly to the image alpha.

    If you want the stepper to use integer values, then just divide the stepper value by it's maximum (expressed as a float) before assigning it:

    view.alpha = myStepper.value / myStepper.maximumValue;

    If the minimum value isn't 0, you need to account for that as well:

    view.alpha = (myStepper.value - myStepper.minimumValue) / myStepper.maximumValue;
  4. larswik thread starter macrumors 68000

    Sep 8, 2006
    I was not aware that the ranges could change from an int value. Once you dropped that key piece of information the rest was down hill and I got it working this morning as expected.

    Thanks for the help.

Share This Page