I am a bit stuck with this. I want to determine two opacity int values. One should increase and the other should decrease by hour.
Imagine a day consists of 4 parts. Night, sunrise, day and sunset. Each of the parts is pictured by
two images that are overlayed. Night has a night image and a sunrise image. At midnight night image has an opacity of lets say 255 while sunrise is actually invisible.
While the time elapses to e.g. 4 o’clock the night image’s opacity gets lower while the sunrise’s opacity increases. At 5 o’clock night is over with an image opacity of 0 while sunrise image has an opacity of 255.
So in each of the 4 parts I want to calculate two opacity values, one increasing the other decreasing. In each part the opacity goes from 255 to 0 and on the other side from 0 to 255.
This is how I seperate the parts in hours
switch (hour) {
//night to sunrise
case 23: case 0: case 1: case 2: case 3: case 4:
[self setSetting:1 andOpacity:opacIn andOpacity:opacOut];
break;
//sunrise to day
case 5: case 6: case 7: case 8: case 9: case 10:
[self setSetting:1 andOpacity:opacIn andOpacity:opacOut];
break;
//day to sunset
case 11: case 12: case 13: case 14: case 15: case 16:
[self setSetting:4 andOpacity:opacIn andOpacity:opacOut];
break;
//sunset to night
case 17: case 18: case 19: case 20: case 21: case 22:
[self setSetting:2 andOpacity:opacIn andOpacity:opacOut];
break;
}
I tried it with this solution
int opacIn = hour * 10;
if (opacIn >= 230) {
opacIn += 25;
}
int opacOut = 255 - opacIn;
The problem with this is that this determines the opacity in reference to the whole day. So it goes from 255 to 0 only once. But I want it to be this way for each part of the day.
Any ideas on this? How could I determine the opacity for each part?
It looks like you need a dash of the old modular arithmetic. Your day is broken up into four segments of six hours each; each hour you want to change some value, and the corresponding hours in every segment have the same value.
So first, divide your target value by the number of steps:
Notice that, if you read the
cases of your switch statement as columns, every value in a column has the same result modulo 6. This is the key; each time you check the time, find out what your position is in the current segment:Now you get your opacity value quite simply:
You could also do away with the
switch: