这里是使用方法,可以使用自动布局,会更新进度条视图大小的,最近优化了一下命名和背景色也可以渐变的逻辑
class GradientProgressController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(self.progressView)
self.progressView.snp.makeConstraints { make in
make.top.equalTo(100)
make.width.height.equalTo(200)
}
self.progressView.setProgress(0.5, animated: true, duration: 1.0)
}
//MARK: - lazy load -
private lazy var progressView: GradientCircleProgressView = {
let view = GradientCircleProgressView(frame: CGRect(x: 0, y: 0, width: 100, height: 100), lineWidth: 10, bgColors: [UIColor(hex: "EDEDED").cgColor, UIColor.red.cgColor], bgLocations: [0.0, 1.0], progressColors: [Colors.mainColor.cgColor, Colors.mainColor.cgColor], progressLocations: [0.0, 1.0])
return view
}()
}
这个是进度条类
class GradientCircleProgressView: UIView {
private var bgColors: [CGColor] = []
private var lineWidth: CGFloat = 0.0
private var progressColors: [CGColor] = []
private var bgLocations: [Double] = []
private var progressLocations: [Double] = []
private var progress: CGFloat = 0.0
override init(frame: CGRect) {
super.init(frame: frame)
}
convenience init(frame: CGRect, lineWidth: CGFloat, bgColors: [CGColor], bgLocations: [Double], progressColors: [CGColor], progressLocations: [Double]) {
self.init(frame: frame)
self.bgColors = bgColors
self.lineWidth = lineWidth
self.progressColors = progressColors
self.bgLocations = bgLocations
self.progressLocations = progressLocations
draw(frame)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(_ rect: CGRect) {
super.draw(rect)
self.bgGradientLayer.removeFromSuperlayer()
self.progressGradientLayer.removeFromSuperlayer()
self.bgGradientLayer.frame = self.bounds
self.bgGradientLayer.colors = self.bgColors
self.bgGradientLayer.locations = self.bgLocations as [NSNumber]
self.bgGradientLayer.startPoint = CGPoint(x: 0, y: 0)
self.bgGradientLayer.endPoint = CGPoint(x: 0, y: 1)
self.layer.addSublayer(self.bgGradientLayer)
let center: CGPoint = CGPoint(x: rect.size.width/2, y: rect.size.height/2)
//画贝塞尔曲线
let bezierPath = UIBezierPath(arcCenter: center, radius: (rect.size.width - self.lineWidth)/2, startAngle: CGFloat(-0.5 * Double.pi), endAngle: CGFloat(1.5 * Double.pi), clockwise: true)
self.bgLayer.frame = self.bounds
self.bgLayer.lineWidth = self.lineWidth
self.bgLayer.fillColor = UIColor.clear.cgColor
self.bgLayer.strokeColor = UIColor.red.cgColor
self.bgLayer.opacity = 0.2
self.bgLayer.lineCap = .round //设置画笔
self.bgLayer.path = bezierPath.cgPath //将曲线添加到layer层
//修改渐变layer层的遮罩,关键代码
self.bgGradientLayer.mask = self.bgLayer
self.progressGradientLayer.frame = self.bounds
self.progressGradientLayer.colors = self.progressColors
self.progressGradientLayer.locations = self.progressLocations as [NSNumber]
self.progressGradientLayer.startPoint = CGPoint(x: 0, y: 0)
self.progressGradientLayer.endPoint = CGPoint(x: 0, y: 1)
self.layer.addSublayer(self.progressGradientLayer)
self.progressLayer.frame = self.bounds
self.progressLayer.lineWidth = self.lineWidth
self.progressLayer.fillColor = UIColor.clear.cgColor
self.progressLayer.strokeColor = UIColor.red.cgColor
self.progressLayer.lineCap = .round //设置画笔
self.progressLayer.path = bezierPath.cgPath
//修改渐变layer层的遮罩,关键代码
self.progressGradientLayer.mask = self.progressLayer
}
public func setProgress(_ progress: Double, animated: Bool = false, duration: Double = 1.0) {
self.progress = progress
if animated {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = 0
animation.toValue = progress
self.progressLayer.add(animation, forKey: nil)
CATransaction.begin()
CATransaction.setDisableActions(true)
// 在这里进行属性更改,例如设置 progressLayer 的 strokeEnd
self.progressLayer.strokeEnd = self.progress
CATransaction.commit()
} else {
CATransaction.begin()
CATransaction.setDisableActions(true)
// 在这里进行属性更改,例如设置 progressLayer 的 strokeEnd
self.progressLayer.strokeEnd = self.progress
CATransaction.commit()
}
}
//MARK: - lazy load -
private lazy var bgLayer: CAShapeLayer = {
let layer = CAShapeLayer()
return layer
}()
private lazy var bgGradientLayer: CAGradientLayer = {
let layer = CAGradientLayer()
return layer
}()
private lazy var progressLayer: CAShapeLayer = {
let layer = CAShapeLayer()
layer.strokeEnd = 0.0
return layer
}()
private lazy var progressGradientLayer: CAGradientLayer = {
let layer = CAGradientLayer()
return layer
}()
}