You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
4.9 KiB
138 lines
4.9 KiB
// |
|
// CircleView.swift |
|
// FunDoHealth |
|
// |
|
// Created by lemo on 2018/6/6. |
|
// Copyright © 2020年 ecell. All rights reserved. |
|
// |
|
|
|
import UIKit |
|
|
|
enum CircleType { |
|
case base |
|
case dottedLine |
|
case doubleLine |
|
} |
|
|
|
class CircleView: UIView { |
|
|
|
/// 进度颜色 |
|
fileprivate var bottomColor = kHexColor(0xFF9833) |
|
/// 进度颜色 |
|
fileprivate var progressColor = kHexColor(0xFF9833) |
|
/// 线宽度 |
|
fileprivate var lineWidth: CGFloat = 0 |
|
/// 圆整体的宽高 |
|
fileprivate var circleWH: CGFloat = 0 |
|
/// 圆的半径 |
|
fileprivate var circleRadius: CGFloat = 0 |
|
/// 圆内数据 |
|
var dataLabel: UILabel! |
|
/// 进度数组 |
|
var shapeLayers: [CAShapeLayer] = [] |
|
/// 圆类型 |
|
var type: CircleType = .base |
|
/// 当前进度 |
|
var progress: Float = 0 |
|
|
|
convenience init(lineWidth: CGFloat, |
|
bottomColor: UIColor, |
|
progressColor: UIColor = .clear, |
|
type: CircleType) { |
|
self.init() |
|
self.lineWidth = lineWidth |
|
self.bottomColor = bottomColor |
|
self.progressColor = progressColor |
|
self.type = type |
|
} |
|
|
|
override func layoutSubviews() { |
|
super.layoutSubviews() |
|
circleWH = width - lineWidth |
|
circleRadius = circleWH / 2.0 |
|
setUpCirle() |
|
} |
|
|
|
/// 更新进度 |
|
/// - Parameter process: 0-1 |
|
func updateProcess(process: Float) { |
|
setUpCirleProcess(process: process) |
|
} |
|
|
|
} |
|
|
|
extension CircleView { |
|
// MARK:- 初始化 |
|
/// 设置圆环 |
|
fileprivate func setUpCirle() { |
|
// 圆形路径 |
|
let cirlePath = UIBezierPath(roundedRect: CGRect(x: lineWidth / 2.0, y: lineWidth / 2.0, width: circleWH, height: circleWH), cornerRadius: circleRadius) |
|
switch type { |
|
case .dottedLine: |
|
let dotteShapLayer = CAShapeLayer() |
|
dotteShapLayer.fillColor = UIColor.clear.cgColor |
|
dotteShapLayer.strokeColor = self.bottomColor.cgColor |
|
dotteShapLayer.lineWidth = lineWidth |
|
dotteShapLayer.path = cirlePath.cgPath |
|
let arr :NSArray = NSArray(array: [3,5]) |
|
dotteShapLayer.lineDashPhase = 1.0 |
|
dotteShapLayer.lineDashPattern = arr as? [NSNumber] |
|
layer.addSublayer(dotteShapLayer) |
|
default: |
|
// 绘制底层圆环 |
|
let bottomCirleShapeLayer = CAShapeLayer() |
|
bottomCirleShapeLayer.path = cirlePath.cgPath |
|
bottomCirleShapeLayer.fillColor = UIColor.clear.cgColor |
|
bottomCirleShapeLayer.strokeColor = self.bottomColor.cgColor |
|
bottomCirleShapeLayer.lineWidth = lineWidth |
|
layer.addSublayer(bottomCirleShapeLayer) |
|
// 绘制进度 |
|
setUpCirleProcess(process: self.progress) |
|
} |
|
} |
|
|
|
/// 圆环进度设置 |
|
fileprivate func setUpCirleProcess(process: Float) { |
|
// 移除上一次进度 |
|
if shapeLayers.count > 0 { |
|
shapeLayers.forEach({ (layer) in |
|
layer.removeFromSuperlayer() |
|
}) |
|
shapeLayers.removeAll() |
|
} |
|
var temProcess = process |
|
if temProcess > 1 { |
|
temProcess = 1 |
|
} |
|
if temProcess < 0 { |
|
temProcess = 0 |
|
} |
|
self.progress = temProcess |
|
// 圆进度路径 |
|
let endAngle = temProcess * Float.pi * 2.0 + 1.5 * Float.pi |
|
let cirlePath = UIBezierPath(arcCenter: CGPoint(x: circleRadius + (lineWidth / 2.0), y: circleRadius + (lineWidth / 2.0)), radius: circleRadius, startAngle: CGFloat(Double.pi) * 1.5, endAngle: CGFloat(endAngle), clockwise: true) |
|
// 绘制进度圆环 |
|
let processCirleShapeLayer = CAShapeLayer() |
|
processCirleShapeLayer.path = cirlePath.cgPath |
|
processCirleShapeLayer.fillColor = UIColor.clear.cgColor |
|
processCirleShapeLayer.strokeColor = progressColor.cgColor |
|
processCirleShapeLayer.lineWidth = lineWidth |
|
processCirleShapeLayer.lineCap = CAShapeLayerLineCap.round |
|
layer.addSublayer(processCirleShapeLayer) |
|
shapeLayers.append(processCirleShapeLayer) |
|
if type == .doubleLine { |
|
// 内进度线 |
|
let internalRadius = circleRadius - lineWidth |
|
let internalCirlePath = UIBezierPath(arcCenter: CGPoint(x: circleRadius + (lineWidth / 2.0), y: circleRadius + (lineWidth / 2.0)), radius: internalRadius, startAngle: CGFloat(Double.pi) * 1.5, endAngle: CGFloat(endAngle), clockwise: true) |
|
let internalCirleShapeLayer = CAShapeLayer() |
|
internalCirleShapeLayer.path = internalCirlePath.cgPath |
|
internalCirleShapeLayer.fillColor = UIColor.clear.cgColor |
|
internalCirleShapeLayer.strokeColor = progressColor.cgColor |
|
internalCirleShapeLayer.lineWidth = 2 |
|
internalCirleShapeLayer.lineCap = CAShapeLayerLineCap.round |
|
layer.addSublayer(internalCirleShapeLayer) |
|
shapeLayers.append(internalCirleShapeLayer) |
|
} |
|
} |
|
|
|
}
|
|
|