-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularProgressView.swift
67 lines (56 loc) · 2.14 KB
/
CircularProgressView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// CircularProgressView.swift
// CircularProgressBar
//
// Created by MAC on 18/1/19.
// Copyright © 2019 cagdaseksi. All rights reserved.
//
import UIKit
class CircularProgressView: UIView {
fileprivate var progressLayer = CAShapeLayer()
fileprivate var trackLayer = CAShapeLayer()
override init(frame: CGRect) {
super.init(frame: frame)
createCircularPath()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
createCircularPath()
}
var progressColor = UIColor.white {
didSet {
progressLayer.strokeColor = progressColor.cgColor
}
}
var trackColor = UIColor.white {
didSet {
trackLayer.strokeColor = trackColor.cgColor
}
}
fileprivate func createCircularPath() {
self.backgroundColor = UIColor.clear
self.layer.cornerRadius = self.frame.size.width/2
let circlePath = UIBezierPath(arcCenter: CGPoint(x: frame.size.width/2, y: frame.size.height/2), radius: (frame.size.width - 1.5)/2, startAngle: CGFloat(-0.5 * .pi), endAngle: CGFloat(1.5 * .pi), clockwise: true)
trackLayer.path = circlePath.cgPath
trackLayer.fillColor = UIColor.clear.cgColor
trackLayer.strokeColor = trackColor.cgColor
trackLayer.lineWidth = 10.0
trackLayer.strokeEnd = 1.0
layer.addSublayer(trackLayer)
progressLayer.path = circlePath.cgPath
progressLayer.fillColor = UIColor.clear.cgColor
progressLayer.strokeColor = progressColor.cgColor
progressLayer.lineWidth = 10.0
progressLayer.strokeEnd = 0.0
layer.addSublayer(progressLayer)
}
func setProgressWithAnimation(duration: TimeInterval, value: Float) {
let animation = CABasicAnimation(keyPath: "strokeEnd")
animation.duration = duration
animation.fromValue = 0
animation.toValue = value
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
progressLayer.strokeEnd = CGFloat(value)
progressLayer.add(animation, forKey: "animateprogress")
}
}