iOS UIStepper value to image.alpha

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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.
Code:
mainImageAlpha = [NSString stringWithFormat:@"%f",[[NSNumber numberWithDouble:[(UIStepper *) sender value]]floatValue]];
Then in VC2 after I read in the plist to an NSDict

Code:
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?

Thanks.
 

Reason077

macrumors 68020
Aug 14, 2007
2,191
682
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?
Dividing the stepper.value by stepper.maximumValue will give you a result between 0.0 and 1.0.
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
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.
Code:
mainImageAlpha = [NSString stringWithFormat:@"%f",[[NSNumber numberWithDouble:[(UIStepper *) sender value]]floatValue]];
Then in VC2 after I read in the plist to an NSDict

Code:
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?

Thanks.

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:


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

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


Code:
view.alpha = (myStepper.value - myStepper.minimumValue) / myStepper.maximumValue;
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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.