订单反向浮动值

时间:2013-10-02 14:27:33

标签: iphone ios objective-c xcode

从值“val”可以看出,我生成0到255之间的值,如何反转此浮点值的顺序?

-(UIColor *)colorForIndexToWhite:(NSInteger)index {    
    NSInteger itemCount = _gradientArrayToWhite.count - 1;
    float val = ((float)index / (float)itemCount) * 0.9;
    UIColor *colorFromHEXCode = [UIColor colorWithHexString:_hexCode];
    NSString *myCount = [NSString stringWithFormat:@"%.f", (colorFromHEXCode.red*255)-(val*255)];
    NSLog(@"Red Value: %.f - val: %.f - Result: %@", colorFromHEXCode.red*255, val*255, myCount);
    return MP_RGB((colorFromHEXCode.red*255)-(val*255), (colorFromHEXCode.green*255)-(val*255), (colorFromHEXCode.blue*255)-(val*255));
}

This is the Output:

    2013-10-02 16:14:02.473 Goolor[993:a0b] Red Value: 70 - val: 0 - Result: 70
    2013-10-02 16:14:02.475 Goolor[993:a0b] Red Value: 70 - val: 26 - Result: 44
    2013-10-02 16:14:02.475 Goolor[993:a0b] Red Value: 70 - val: 51 - Result: 19
    2013-10-02 16:14:02.476 Goolor[993:a0b] Red Value: 70 - val: 76 - Result: -6
    2013-10-02 16:14:02.477 Goolor[993:a0b] Red Value: 70 - val: 102 - Result: -32
    2013-10-02 16:14:02.478 Goolor[993:a0b] Red Value: 70 - val: 128 - Result: -58
    2013-10-02 16:14:02.478 Goolor[993:a0b] Red Value: 70 - val: 153 - Result: -83
    2013-10-02 16:14:02.479 Goolor[993:a0b] Red Value: 70 - val: 178 - Result: -108
    2013-10-02 16:14:02.480 Goolor[993:a0b] Red Value: 70 - val: 204 - Result: -134
    2013-10-02 16:14:02.480 Goolor[993:a0b] Red Value: 70 - val: 230 - Result: -160

1 个答案:

答案 0 :(得分:0)

而不是:

(color*255)-(val*255)

color * val * 255

顺便说一下。我想你只是从某个地方复制粘贴它。现在它生成的值从255到50左右,而不是0 ......你在浮点数上有0.9f + index将永远不会等于itemCount。