0

试图使条形图成为渐变色,但找不到任何有关如何执行此操作的文档。看起来这是正确的功能。只需要关于在括号中输入什么内容的帮助。也不关心什么颜色。

func barGradientForBarChartView(barChartView: JBBarChartView!) -> CAGradientLayer! {


}

谢谢您的帮助!

4

2 回答 2

0

设法弄清楚这一点。想我会发布代码以防其他人需要。请记住,它涵盖了所有的酒吧。您不能像实心条那样通过索引将它们分开。

func barGradientForBarChartView(barChartView: JBBarChartView) -> CAGradientLayer {
    let topColor = UIColor.orangeColor()
    let bottomColor = UIColor.blueColor()

    let gradientColors: [CGColor] = [topColor.CGColor, bottomColor.CGColor]
    let gradientLocations: [Float] = [0.0, 1.0]

    let gradientLayer: CAGradientLayer = CAGradientLayer()
    gradientLayer.colors = gradientColors
    gradientLayer.locations = gradientLocations

    return gradientLayer
}
于 2016-01-31T06:12:39.640 回答
0

仅供参考,标题包含必要的文档。同样,自述文件是一个很好的起点。

/**
 *  If you already implement barChartView:barViewAtIndex: delegate - this   method has no effect.
 *  If a custom UIView isn't supplied and barChartView:colorForBarViewAtIndex:  isn't implemented, then
 *  a gradient layer may be supplied to be used across all bars within the chart.
 *
 *  Default: black color.
 *
 *  @param barChartView     The bar chart object requesting this information.
 *
 *  @return The gradient layer to be used as a mask over all bars within the chart.
 */

最后,您可以通过使用 barViewAtIndex: 并返回带有渐变的自定义视图,在每个柱的基础上创建渐变。

样本:

- (UIView *)barChartView:(JBBarChartView *)barChartView barViewAtIndex:(NSUInteger)index
{
    UIView *customBarView = [[UIView alloc] init];
    CAGradientLayer *gradient = [CAGradientLayer new];
    gradient.startPoint = CGPointMake(0.0, 0.0);
    gradient.endPoint = CGPointMake(1.0, 0.0);
    gradient.colors = @[(id)[UIColor redColor].CGColor, (id)[UIColor greenColor].CGColor];
    [customBarView.layer insertSublayer:gradient atIndex:0];
    return customBarView;
}

当然,customView 应该是 UIView 的子类,您应该实现 layoutSubviews 以正确设置渐变的框架,但您明白了。

于 2016-02-26T19:40:07.080 回答