【问题标题】:UIBeziepath with Color gradient带有颜色渐变的 UIBezierpath
【发布时间】:2017-03-07 15:02:49
【问题描述】:

我正在画一个圆圈,只要圆圈只有一种颜色,用力就可以了。但我需要有两种颜色,位置 0 是绿色,位置 360 是红色。位置 180 应该是半绿半红。

这里是绘制我的圆圈的代码

let center = CGPoint(x: bounds.midX, y: bounds.midY)

        // Calculate the center difference between the end and start angle
        let angleDiff: CGFloat = endAngle.toRads - startAngle.toRads
        // Calculate how much we should draw depending on the value set
        let arcLenPerValue = angleDiff / CGFloat(maxValue)
        // The inner end angle some basic math is done
        let innerEndAngle = arcLenPerValue * CGFloat(value) + startAngle.toRads

        // The radius for style 1 is set below
        // The radius for style 1 is a bit less than the outer, this way it looks like its inside the circle
        var radiusIn = (max(bounds.width - outerRingWidth*2 - innerRingSpacing, bounds.height - outerRingWidth*2 - innerRingSpacing)/2) - innerRingWidth/2

        // If the style is different, mae the radius equal to the outerRadius
        if viewStyle >= 2 {
            radiusIn = (max(bounds.width, bounds.height)/2) - (outerRingWidth/2)
        }


        // Start drawing
        let innerPath = UIBezierPath(arcCenter: center,
                                     radius: radiusIn,
                                     startAngle: startAngle.toRads,
                                     endAngle: innerEndAngle,
                                     clockwise: true)
        innerPath.lineWidth = innerRingWidth
        innerPath.lineCapStyle = innerCapStyle
        innerRingColor.setStroke()
        innerPath.stroke()

你知道如何应用渐变吗?

【问题讨论】:

    标签: ios gradient uibezierpath


    【解决方案1】:

    路径通常使用单一颜色绘制。

    如果您想使用渐变绘制路径,您可以创建一个 CAShapeLayer 并将您的路径安装到其中,然后将该路径设置为渐变层上的蒙版。

    毫无疑问,您也可以使用核心图形和混合模式来做到这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多