首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jbchartview中的梯度

jbchartview中的梯度
EN

Stack Overflow用户
提问于 2015-12-25 06:20:59
回答 2查看 100关注 0票数 0

试图使条形图成为渐变颜色,但找不到关于如何这样做的任何文档。看上去这是正确的玩法。只是需要帮助在括号中键入什么。也不关心什么颜色。

代码语言:javascript
复制
func barGradientForBarChartView(barChartView: JBBarChartView!) -> CAGradientLayer! {


}

谢谢你的帮助!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-01-31 06:12:39

想出了这个办法。我想我会把代码发出去以防其他人需要。记住它覆盖了所有的酒吧。你不能像用实心条那样按索引把它们分开。

代码语言:javascript
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2016-02-26 19:40:07

只是一个FYI,标题包含必要的文档。同样,自述文件也是一个很好的起点。

代码语言:javascript
复制
/**
 *  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:并返回带有渐变的自定义视图,您可以在每个栏上创建一个渐变。

示例:

代码语言:javascript
复制
- (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来正确地设置梯度的框架,但是您知道了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34460528

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档