在SWIFT中绘制圆形线段进度 [英] Draw a circular segment progress in SWIFT

查看:118
本文介绍了在SWIFT中绘制圆形线段进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用每个切片都是弧形的切片创建循环进度。



我的代码基于以下答案:



请帮助



致谢

解决方案

您可以使用圆形路径并设置strokeStart和StrokeEnd。像这样的东西:

  let circlePath = UIBezierPath(ovalInRect:CGRect(x:200,y:200,width:150,height :150))
var个段:[CAShapeLayer] = []
让segmentAngle:CGFloat =(360 * 0.125)/ 360

for var i = 0;我< 8; i ++ {
let circleLayer = CAShapeLayer()
circleLayer.path = circlePath.CGPath

//起始角度为线段数*线段角度
circleLayer.strokeStart = segmentAngle * CGFloat(i)

//结束角是起点加上一个线段,减去一点可形成一个间隙
//您必须使用此值才能获得它可以正确地查看您需要的尺寸
让gapSize:CGFloat = 0.008
circleLayer.strokeEnd = circleLayer.strokeStart + segmentAngle-gapSize

circleLayer.lineWidth = 10
circleLayer.strokeColor = UIColor(红色:0,绿色:0.004,蓝色:0.549,alpha:1).CGColor
circleLayer.fillColor = UIColor.clearColor()。CGColor

//将分段添加到segments数组和视图
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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆