在 SWIFT 中绘制圆形段进度 [英] Draw a circular segment progress in SWIFT
本文介绍了在 SWIFT 中绘制圆形段进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用切片创建一个循环进度,其中每个切片都是一个弧.
我的代码基于这个答案:
请帮忙
问候
解决方案
您可以使用圆形路径并设置strokeStart 和StrokeEnd.像这样:
let circlePath = UIBezierPath(ovalInRect: CGRect(x: 200, y: 200, width: 150, height: 150))var 段:[CAShapeLayer] = []让segmentAngle:CGFloat = (360 * 0.125)/360对于 var i = 0;我<8;我++ {让 circleLayer = CAShapeLayer()circleLayer.path = circlePath.CGPath//起始角度是线段数 * 线段角度circleLayer.strokeStart = segmentAngle * CGFloat(i)//结束角度是起点加一段,减去一点就形成一个间隙//你必须使用这个值才能让它看起来像你需要的大小让间隙大小:CGFloat = 0.008circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle - gapSizecircleLayer.lineWidth = 10circleLayer.strokeColor = UIColor(red:0, green:0.004, blue:0.549, alpha:1).CGColorcircleLayer.fillColor = UIColor.clearColor().CGColor//将段添加到段数组和视图中segment.insert(circleLayer, atIndex: i)view.layer.addSublayer(segments[i])}
I would like to create a circular progress with slices where each slice is an arc.
I based my code on this answer: Draw segments from a circle or donut
But I don't know how to copy it and rotate it 10 times. And I would like to color it following a progress variable (in percent).
EDIT: I would like something like this
Any help please
Regards
解决方案
You could use a circular path and set the strokeStart and StrokeEnd. Something like this:
let circlePath = UIBezierPath(ovalInRect: CGRect(x: 200, y: 200, width: 150, height: 150))
var segments: [CAShapeLayer] = []
let segmentAngle: CGFloat = (360 * 0.125) / 360
for var i = 0; i < 8; i++ {
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath
// start angle is number of segments * the segment angle
circleLayer.strokeStart = segmentAngle * CGFloat(i)
// end angle is the start plus one segment, minus a little to make a gap
// you'll have to play with this value to get it to look right at the size you need
let gapSize: CGFloat = 0.008
circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle - gapSize
circleLayer.lineWidth = 10
circleLayer.strokeColor = UIColor(red:0, green:0.004, blue:0.549, alpha:1).CGColor
circleLayer.fillColor = UIColor.clearColor().CGColor
// add the segment to the segments array and to the view
segments.insert(circleLayer, atIndex: i)
view.layer.addSublayer(segments[i])
}
这篇关于在 SWIFT 中绘制圆形段进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文