Xcode 图表视图中的渐变

Xcode 图表视图中的渐变,xcode,swift,jbchartview,Xcode,Swift,Jbchartview,试图使酒吧梯度颜色,但找不到任何文件如何做到这一点。看起来这是正确的函数。只需要在括号中输入内容的帮助。也不关心什么颜色 func barGradientForBarChartView(barChartView: JBBarChartView!) -> CAGradientLayer! { } 谢谢你的帮助 设法解决了这个问题。我想我会发布代码以防其他人需要。只要记住它涵盖了所有的酒吧。不能像用实心条那样用索引将它们分开 func barGradientForBarChartView

试图使酒吧梯度颜色,但找不到任何文件如何做到这一点。看起来这是正确的函数。只需要在括号中输入内容的帮助。也不关心什么颜色

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


}

谢谢你的帮助

设法解决了这个问题。我想我会发布代码以防其他人需要。只要记住它涵盖了所有的酒吧。不能像用实心条那样用索引将它们分开

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
}

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

/**
 *  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来正确设置渐变的帧,但是您明白了