【问题标题】:Understanding CAGradient locations?了解 CAGradient 位置?
【发布时间】:2011-03-24 14:38:41
【问题描述】:

我正在创建一个带有 CAGradient 层的简单条形图。我有 6 种颜色希望在图层上均匀分布。

NSMutableArray *colors = [NSArray arrayWithObjects:
                          (id)[[UIColor colorWithRed:0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:.95] CGColor],
                          (id)[[UIColor colorWithRed:204.0/255.0 green:153.0/255.0 blue:0/255.0 alpha:.95]CGColor],
                         (id)[ [UIColor colorWithRed:204.0/255.0 green:102.0/255.0 blue:51.0/255.0 alpha:.95]CGColor],
                         (id)[ [UIColor colorWithRed:204.0/255.0 green:51.0/255.0 blue:51.0/255.0 alpha:.95]CGColor],
                         (id)[ [UIColor blackColor]CGColor],nil];

NSMutableArray *locations = [NSArray arrayWithObjects:
                                [NSNumber numberWithFloat:0.0],
                                [NSNumber numberWithInt:0.14],
                                [NSNumber numberWithInt:0.28],
                                [NSNumber numberWithInt:0.42],
                                [NSNumber numberWithInt:0.56],
                                [NSNumber numberWithInt:0.70],
                                [NSNumber numberWithInt:0.84],
                                [NSNumber numberWithInt:1.0],nil];

并用 :

实现它们
 gradientLayer_.colors =colors;
 gradientLayer_.locations=locations;

但我得到一个完全黑条。当我删除位置时,我可以完美地看到渐变,但没有很好地分割。

我觉得我不太了解这些位置。

有什么帮助吗?

【问题讨论】:

    标签: ios calayer gradient


    【解决方案1】:
    1. 您在多个停靠点定义上使用[NSNumber numberWithInt:]。使用[NSNumber numberWithFloat:]
    2. 颜色数应与站数相同。您有 5 种颜色和 8 个停靠点。添加 3 种颜色或删除 3 个停靠点。

    【讨论】:

    • 谢谢。你知道我发现在 90% 的情况下它总是一个非常简单的解决方案。
    猜你喜欢
    • 2015-02-27
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-10
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多