我想以如下代码所示的格式创建一个函数,用于在定义的颜色之间淡去。这个函数的目的是根据数据创建的时间改变表格视图单元格的颜色;NSDate。这个函数应该只考虑一天中的时间;与2015-07-06T12:00:00颜色相同
@implementation NSDate (COLOR_)
- (UIColor *)dayNightColorByTimeOfDay {
}
@end
我想要"渐变"的颜色是:
- 12 # 0525 ff 6 # FFF199 p
- 12 # FFD005
- 6 p # 059 cff
- ,然后返回到12a。
表中的每个单元格代表这个数组的一个实例:
array {
...
NSString title,
NSDate date
...
};
数组中的数据。date总是设置为[NSDate date]。现在你可以看出self, NSDate的数据类型,是被告变量
答案包含一些想法。首先是颜色插值。RGB表示可以通过插入组件来相当简单地插入,如下所示:
// take two bounding colors and answer one that is pct distance between them
- (UIColor *)colorBetween:(UIColor *)colorA and:(UIColor *)colorB distance:(CGFloat)pct {
CGFloat aR, aG, aB, aA;
[colorA getRed:&aR green:&aG blue:&aB alpha:&aA];
CGFloat bR, bG, bB, bA;
[colorB getRed:&bR green:&bG blue:&bB alpha:&bA];
CGFloat rR = (1.0-pct)*aR + pct*bR;
CGFloat rG = (1.0-pct)*aG + pct*bG;
CGFloat rB = (1.0-pct)*aB + pct*bB;
CGFloat rA = (1.0-pct)*aA + pct*bA;
return [UIColor colorWithRed:rR green:rG blue:rB alpha:rA];
}
我们还需要插入时间,这可以通过将时间表示为午夜过后的分钟来完成。
- (NSInteger)minutesSinceMidnightOfDate:(NSDate *)date {
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSIntegerMax fromDate:date];
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *midnight = [[NSCalendar currentCalendar] dateFromComponents:components];
NSDateComponents *diff = [[NSCalendar currentCalendar] components:NSCalendarUnitMinute fromDate:midnight toDate:date options:0];
return [diff minute];
}
我们需要表示插值的参数,在这里尽可能直接地从问题中给出的参数来完成…
// macro to convert hex value to UIColor
#define UIColorFromRGB(rgbValue)
[UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0
green:((float)((rgbValue & 0x00FF00) >> 8))/255.0
blue:((float)((rgbValue & 0x0000FF) >> 0))/255.0
alpha:1.0]
现在,要做这项工作,找到给定时间前后的时间(以午夜过后的分钟为单位),得到与这些时间对应的颜色,用与前后时间之间的时间距离相匹配的比例插入颜色。
- (UIColor *)colorForDate:(NSDate *)date {
// look up the bounds of interpolation here
NSArray *wheel = @[ @[ @0, UIColorFromRGB(0x0525FF) ],
@[ @360, UIColorFromRGB(0xFFF199) ],
@[ @720, UIColorFromRGB(0xFFD005) ],
@[ @1080, UIColorFromRGB(0x059CFF) ],
@[ @1440, UIColorFromRGB(0x0525FF) ]];
NSInteger m = [self minutesSinceMidnightOfDate:date];
// find the index in wheel where the minute bound exceeds our date's minutes (m)
NSInteger wheelIndex = 0;
for (NSArray *pair in wheel) {
NSInteger timePosition = [pair[0] intValue];
if (m < timePosition) {
break;
}
wheelIndex++;
}
// wheelIndex will always be in 1..4, get the pair of bounds at wheelIndex
// and the preceding pair (-1).
NSArray *priorPair = wheel[wheelIndex-1];
NSArray *pair = wheel[wheelIndex];
CGFloat priorMinutes = [priorPair[0] intValue];
CGFloat minutes = [pair[0] intValue];
// this is how far we are between the bounds pairs
CGFloat minutesPct = ((float)m - priorMinutes) / (minutes - priorMinutes);
// and the colors for the bounds pair
UIColor *priorColor = priorPair[1];
UIColor *color = pair[1];
// call the color interpolation
return [self colorBetween:priorColor and:color distance:minutesPct];
}