class CirCle:UIView{
//进度条宽度
let lineWidth: CGFloat = 2
//进度槽颜色
let trackColor = UIColor.black
//进度条颜色
let progressColoar = UIColor.red
//进度槽
let trackLayer = CAShapeLayer()
//进度条
let progressLayer = CAShapeLayer()
//进度条路径(整个圆圈)
let path = UIBezierPath()
//当前进度
@IBInspectable var progress: Double = 0 {
didSet {
if progress > 100 {
progress = 100
}else if progress < 0 {
progress = 0
}
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func draw(_ rect: CGRect) {
//获取整个进度条圆圈路径
path.addArc(withCenter: CGPoint(x: bounds.midX, y: bounds.midY),
radius: bounds.size.width/2 - lineWidth,
startAngle: angleToRadian(-90), endAngle: angleToRadian(270), clockwise: true)
//绘制进度槽
trackLayer.frame = bounds
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.strokeColor = trackColor.cgColor
trackLayer.lineWidth = lineWidth
trackLayer.path = path.cgPath
layer.addSublayer(trackLayer)
//绘制进度条
progressLayer.frame = bounds
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.strokeColor = progressColoar.cgColor
progressLayer.lineWidth = lineWidth
progressLayer.path = path.cgPath
progressLayer.strokeStart = 0
progressLayer.strokeEnd = CGFloat(progress)/100.0
layer.addSublayer(progressLayer)
}
//设置进度(可以设置是否播放动画)
func setProgress(_ pro: Double,animated anim: Bool) {
setProgress(pro, animated: anim, withDuration: 0.55)
}
//设置进度(可以设置是否播放动画,以及动画时间)
func setProgress(_ pro: Double,animated anim: Bool, withDuration duration: Double) {
progress = pro
//进度条动画
CATransaction.begin()
CATransaction.setDisableActions(!anim)
CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name:
CAMediaTimingFunctionName.easeInEaseOut))
CATransaction.setAnimationDuration(duration)
progressLayer.strokeEnd = CGFloat(progress)/100.0
CATransaction.commit()
}
//将角度转为弧度
fileprivate func angleToRadian(_ angle: Double)->CGFloat {
return CGFloat(angle/Double(180.0) * M_PI)
}
}
swift-圆形进度条的简单封装view类
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...