【问题标题】:How to fade between define colors with a dependent variable returning UIColor如何使用返回 UIColor 的因变量在定义颜色之间淡入淡出
【发布时间】:2015-07-07 21:56:38
【问题描述】:

我想创建一个函数,格式如下面的代码所示,在定义的颜色之间渐变。这个函数的目的是根据数据的创建时间改变表格视图单元格的颜色; NSD日期。这个函数应该只考虑一天中的时间; 2015-07-07T12:00:00 将具有与 2015-07-06T12:00:00 相同的颜色。

@implementation NSDate (COLOR_)

- (UIColor *)dayNightColorByTimeOfDay {

}

@end

我想要“淡化”的颜色是:

  • 12a #0525FF
  • 6a #FFF199
  • 12p #FFD005
  • 6p #059CFF
  • 然后回到 12a。

一个例子是传递给函数的日期,dateValue = 3pm,将返回两种颜色之间的颜色,12p 和 6p。我不想让它动画化。

表格中的每个单元格代表这个数组的一个实例:

array {
  ...
  NSString title,
  NSDate date
  ...
};

array.date 中的数据始终设置为 [NSDate date]。正如您现在所知道的,NSDate 的数据类型是被告变量。

【问题讨论】:

    标签: objective-c nsdate uicolor nsdatecomponents


    【解决方案1】:

    答案包含一些想法。首先是颜色的插值。 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];
    }
    

    【讨论】:

    • 这太棒了!我能否使用任何其他十六进制或任意数量的对修改声明 NSArray *wheel = ..?就像我想添加更多当天的定义颜色一样。
    • 当然。只要第一个时间值为0,最后一个为1440。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 2015-04-07
    • 1970-01-01
    • 2018-07-24
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多