由于项目需求,需要做个这种的东西来实现某种功能,我在网上找到了很多资料却始终不行,最终和本公司的安卓商量出来这个方法,基本思想就是画四十八个扇形,围绕圆的中心绘制成这样一个圆环,如图所示:
我是继承 UIControl 类来实现的,主要就是重写它的 drawRect 方法,来绘制这个"圆", 不多说,看代码:
override func draw(_ rect: CGRect) {
var blockBeginAngle = degreesToRandinas(deg: beginAngle)
var blockEndAngle = degreesToRandinas(deg: beginAngle + blockAngle)
let context = UIGraphicsGetCurrentContext()
for index in 0..<blockCount {
drawImage(context: context!,
startAngle: blockBeginAngle,
endAngle: blockEndAngle,
colorIndex: index)
// 下一块的起始角度和终止角度
blockBeginAngle = blockBeginAngle + degreesToRandinas(deg: blockAngle + sweepAngle)
blockEndAngle = blockEndAngle + degreesToRandinas(deg: blockAngle + sweepAngle)
}
}
private func drawImage(context: CGContext,
startAngle: CGFloat,
endAngle: CGFloat,
colorIndex: Int) {
// 画扇形
context.addArc(center: CGPoint(x: pointX, y: pointY),
radius: outerRadius,
startAngle: startAngle,
endAngle: endAngle,
clockwise: false)
context.addArc(center: CGPoi