带颜色渐变的UIBeziepath
问题描述:
只要圆圈只有一种颜色,我正在绘制一个圆圈并发挥效果。但我需要两种颜色,其中位置0是绿色,位置360是红色。位置180应该是半绿半红。带颜色渐变的UIBeziepath
这里,吸引了我的圈子
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()
你知道如何运用渐变的代码?
答
路径通常使用单一颜色绘制。
如果要使用渐变绘制路径,可以创建CAShapeLayer并将路径安装到其中,然后使该路径成为渐变图层上的遮罩。
您无疑也可以使用Core Graphics和混合模式来做到这一点。