Skip to content

Commit 442c9fb

Browse files
author
Jasper
committed
增加部分进度条支持
修复一些问题
1 parent 78699c0 commit 442c9fb

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

CYUtilProjectSwift/CYUtilProjectSwift/CYUtilProjectSwift/ViewController.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,30 @@ class ViewController: UIViewController {
2525
// Do any additional setup after loading the view, typically from a nib.
2626

2727
// let progress = CYLineProgressBar(frame: CGRect(x: 10, y: 150, width: 100, height: 30))
28-
// let progress = CYCycleProgressBar(frame: CGRect(x: 10, y: 150, width: 100, height: 100))
28+
let progress = CYCycleProgressBar(startAngle: M_PI * 3 / 2,
29+
cycleRadius: Double(48.5),
30+
cycleCenter: CGPoint(x: 50, y: 50),
31+
barWidth: 3,
32+
frame: CGRect(x: 10, y: 150, width: 100, height: 100))
33+
progress.color = UIColor.clear
34+
progress.completionColor = UIColor.red
35+
progress.lineCap = kCALineCapButt
2936
// let progress = CYCycleProgressBar(startAngle: M_PI, cycleRadius: 48, cycleCenter: CGPoint(x: 50, y: 50), frame: CGRect(x: 10, y: 150, width: 100, height: 100))
3037
// let progress = CYArcProgressBar(frame: CGRect(x: 10, y: 150, width: 100, height: 100))
31-
let progress = CYArcProgressBar(startAngle: 2.8, endAngle: 6.8, arcRadius: 48, arcCenter: CGPoint(x: 50, y: 50), frame: CGRect(x: 10, y: 150, width: 100, height: 100))
38+
// let progress = CYArcProgressBar(startAngle: 2.8, calculateStartAngle: 3.5, endAngle: 6.8, arcRadius: 48, arcCenter: CGPoint(x: 50, y: 50), frame: CGRect(x: 10, y: 150, width: 100, height: 100))
39+
// let progress = CYArcProgressBar(startAngle: 2.8,
40+
// calculateStartAngle: 3.5,
41+
// endAngle: 6.8,
42+
// arcRadius: 48,
43+
// arcCenter: CGPoint(x: 50, y: 50),
44+
// frame: CGRect(x: 10, y: 150, width: 100, height: 100))
3245
progress.backgroundColor = UIColor.green
3346
let progressHeader = UIView(frame: CGRect(x: 0, y: 0, width: 8, height: 8))
3447
progressHeader.backgroundColor = UIColor.red
3548
progressHeader.layer.cornerRadius = 4
3649
progress.progressHeaderView = progressHeader
3750
self.view.addSubview(progress)
38-
progress.setProgress(progress: 0.75, animated: true)
51+
progress.setProgress(progress: 0.3, animated: true)
3952
progressView = progress
4053
}
4154

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/CYProgressUtils/CYArcProgressBar.swift

Lines changed: 53 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,66 @@ import UIKit
1818

1919
class CYArcProgressBar: CYBaseProgressBar {
2020

21+
// 整个bar的其实角度
2122
private(set) var startAngle: Double
23+
// 计算进度的起始角度,在calculateStartAngle和startAngle之间的部分都是已完成状态
24+
private(set) var calculateStartAngle: Double
2225
private(set) var endAngle: Double
2326
private(set) var arcRadius: Double
2427
private(set) var arcCenter: CGPoint
2528

2629
private var totalAngle: Double
2730

2831
init(startAngle: Double,
32+
calculateStartAngle: Double,
2933
endAngle: Double,
3034
arcRadius: Double,
3135
arcCenter: CGPoint,
3236
barWidth: Double,
3337
frame: CGRect) {
38+
3439
self.startAngle = startAngle
40+
self.calculateStartAngle = calculateStartAngle
3541
self.endAngle = endAngle
3642
self.arcRadius = arcRadius
3743
self.arcCenter = arcCenter
3844

3945
// compute total Angle
40-
totalAngle = endAngle - startAngle
46+
totalAngle = endAngle - calculateStartAngle
4147
if totalAngle < 0 {
4248
totalAngle += 2 * M_PI
4349
}
4450
super.init(barWidth: barWidth, frame: frame)
4551
}
4652

47-
override convenience init(barWidth: Double, frame: CGRect) {
48-
self.init(startAngle: 0,
49-
endAngle: M_PI * 1.5,
50-
arcRadius: Double(min(frame.width, frame.height)) / 2 - barWidth / 2,
51-
arcCenter: CGPoint(x: frame.width / 2.0, y: frame.height / 2.0),
53+
convenience init(startAngle: Double,
54+
calculateStartAngle: Double,
55+
endAngle: Double,
56+
arcRadius: Double,
57+
arcCenter: CGPoint,
58+
frame: CGRect) {
59+
60+
self.init(startAngle: startAngle,
61+
calculateStartAngle: calculateStartAngle,
62+
endAngle: endAngle,
63+
arcRadius: arcRadius,
64+
arcCenter: arcCenter,
65+
barWidth: 4,
66+
frame: frame)
67+
}
68+
69+
convenience init(startAngle: Double,
70+
endAngle: Double,
71+
arcRadius: Double,
72+
arcCenter: CGPoint,
73+
barWidth: Double,
74+
frame: CGRect) {
75+
76+
self.init(startAngle: startAngle,
77+
calculateStartAngle: startAngle,
78+
endAngle: endAngle,
79+
arcRadius: arcRadius,
80+
arcCenter: arcCenter,
5281
barWidth: barWidth,
5382
frame: frame)
5483
}
@@ -65,6 +94,15 @@ class CYArcProgressBar: CYBaseProgressBar {
6594
barWidth: 4.0,
6695
frame: frame)
6796
}
97+
98+
override convenience init(barWidth: Double, frame: CGRect) {
99+
self.init(startAngle: 0,
100+
endAngle: M_PI * 1.5,
101+
arcRadius: Double(min(frame.width, frame.height)) / 2 - barWidth / 2,
102+
arcCenter: CGPoint(x: frame.width / 2.0, y: frame.height / 2.0),
103+
barWidth: barWidth,
104+
frame: frame)
105+
}
68106

69107
required init?(coder aDecoder: NSCoder) {
70108

@@ -86,8 +124,8 @@ class CYArcProgressBar: CYBaseProgressBar {
86124

87125
private func refreshHeaderPosition() {
88126

89-
let endFactorW = cos(progress * totalAngle + startAngle)
90-
let endFactorH = sin(progress * totalAngle + startAngle)
127+
let endFactorW = cos(progress * totalAngle + calculateStartAngle)
128+
let endFactorH = sin(progress * totalAngle + calculateStartAngle)
91129
progressHeaderView?.center = CGPoint(x: endFactorW * arcRadius + Double(arcCenter.x),
92130
y: endFactorH * arcRadius + Double(arcCenter.y))
93131
}
@@ -107,20 +145,20 @@ class CYArcProgressBar: CYBaseProgressBar {
107145
let path = UIBezierPath(arcCenter: arcCenter,
108146
radius: CGFloat(arcRadius),
109147
startAngle: CGFloat(startAngle),
110-
endAngle: CGFloat(progress * totalAngle + startAngle),
148+
endAngle: CGFloat(progress * totalAngle + calculateStartAngle),
111149
clockwise: true)
112150
completionLayer?.path = path.cgPath
113-
// completedPath = path
151+
completedPath = path.cgPath
114152
}
115153

116154
override func addAnimation() {
117155
super.addAnimation()
118156

119-
// let animation = CAKeyframeAnimation(keyPath: "position")
120-
// animation.path = completedPath?.cgPath
121-
// animation.calculationMode = kCAAnimationPaced
122-
// animation.duration = 2
123-
// progressHeaderView?.layer.add(animation, forKey: nil)
157+
let animation = CAKeyframeAnimation(keyPath: "position")
158+
animation.path = completedPath
159+
animation.calculationMode = kCAAnimationPaced
160+
animation.duration = 2
161+
progressHeaderView?.layer.add(animation, forKey: nil)
124162
}
125163
}
126164

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/CYProgressUtils/CYBaseProgressBar.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class CYBaseProgressBar: UIView {
1515
private(set) var fullLayer: CAShapeLayer?
1616
private(set) var completionLayer: CAShapeLayer?
1717

18-
private var completedPath: UIBezierPath?
18+
var completedPath: CGPath?
1919

2020
// progress, use
2121
private(set) var progress = 0.0

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/CYProgressUtils/CYCycleProgressBar.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ class CYCycleProgressBar: CYBaseProgressBar {
9898
endAngle: CGFloat(progress * 2 * M_PI + startAngle),
9999
clockwise: true)
100100
completionLayer?.path = path.cgPath
101+
completedPath = path.cgPath
101102
}
102103

103104
override func addAnimation() {
104105
super.addAnimation()
105106

106-
// let animation = CAKeyframeAnimation(keyPath: "position")
107-
// animation.path = completedPath?.cgPath
108-
// animation.calculationMode = kCAAnimationPaced
109-
// animation.duration = 2
110-
// progressHeaderView?.layer.add(animation, forKey: nil)
107+
let animation = CAKeyframeAnimation(keyPath: "position")
108+
animation.path = completedPath
109+
animation.calculationMode = kCAAnimationPaced
110+
animation.duration = 2
111+
progressHeaderView?.layer.add(animation, forKey: nil)
111112
}
112113
}

CYUtilProjectSwift/CYUtilProjectSwift/CYUtils/CYProgressUtils/CYLineProgressBar.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,17 @@ class CYLineProgressBar: CYBaseProgressBar {
7575
path.addLine(to: CGPoint(x: (endPoint.x - startPoint.x) * CGFloat(progress) + startPoint.x,
7676
y: (endPoint.y - startPoint.y) * CGFloat(progress) + startPoint.y))
7777
completionLayer?.path = path
78+
completedPath = path
7879
}
7980

8081
override func addAnimation() {
8182
super.addAnimation()
8283

83-
// let animation = CAKeyframeAnimation(keyPath: "position")
84-
// animation.path = completedPath?.cgPath
85-
// animation.calculationMode = kCAAnimationPaced
86-
// animation.duration = 2
87-
// progressHeaderView?.layer.add(animation, forKey: nil)
84+
let animation = CAKeyframeAnimation(keyPath: "position")
85+
animation.path = completedPath
86+
animation.calculationMode = kCAAnimationPaced
87+
animation.duration = 2
88+
progressHeaderView?.layer.add(animation, forKey: nil)
8889
}
8990

9091
}

0 commit comments

Comments
 (0)