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

//
// 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)
}
}
}