I have UISlider. By increasing it’s value, it needs to decrease alpha of UIImage from 1 to 0 (UIImage situated on the same view).
max-min values:
UISlider: 0 – 100
UIImage.alpha = 1 – 0
If I understand right, question is to write an algorhythm, what will set corresponding relations to pairs of values, e.g.: 0,99 – 0,01, 0,98 – 0,02 …
Solution
If you want slider value 100 to be alpha 1.0, use:
valueFromSlider / maxValueOfSliderIf you want slider value 0 to be alpha 1.0, use:
1 - (valueFromSlider / maxValueOfSlider)Why does this work?
You’re simply getting a percentage of how far along the slider is, and applying that percentage as the alpha (which works in percentages anyway). The second solution that uses
1 - ...is simply calculating the inverse value.With this calculation, your
UISlidercan have any range of values, from0-5or0-25000, and the correct alpha will still be calculated. The only difference being is that you have finer granularity of control of the alpha with the larger range, which makes sense, you have 25000 possible values as opposed to 5 (assuming discrete values, sliders can be continuous).