-
Notifications
You must be signed in to change notification settings - Fork 5
/
RTProgressBar.swift
158 lines (141 loc) · 4.92 KB
/
RTProgressBar.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// RTProgressBar.swift
// RTProgressBar
//
// Created by Daniyar Salakhutdinov on 01.08.16.
// Copyright © 2016 Daniyar Salakhutdinov. All rights reserved.
//
import Cocoa
import QuartzCore
let kRTProgressBarMaxProgressValue: Double = 100
open class RTProgressBar: NSView {
fileprivate let sublayer = CALayer()
fileprivate let inlayer = CAGradientLayer()
fileprivate let lock = NSLock()
fileprivate var progressValue: Double = 0
open var progress: Double { // from 0 to 100
set(value) {
setProgress(value, animated: false)
}
get {
return progressValue
}
}
open var color: NSColor = NSColor.blue {
didSet {
updateColor()
}
}
open var animationColor: NSColor? = nil {
didSet {
updateColor()
}
}
open var backgroundColor: NSColor = NSColor.clear {
didSet {
layer?.backgroundColor = backgroundColor.cgColor
}
}
open var animating: Bool = false {
didSet {
if !indeterminate && animating {
return
}
updateColor()
if animating {
inlayer.backgroundColor = nil
inlayer.locations = [0, 0.001, 0.009, 0.1, 1.1]
let animation = CABasicAnimation(keyPath: "locations")
animation.repeatCount = .infinity
animation.duration = 1.5
animation.fromValue = inlayer.locations
animation.toValue = [0, 1, 1.1, 1.11, 1.12]
inlayer.add(animation, forKey: "locations")
} else {
inlayer.removeAllAnimations()
inlayer.locations = nil
inlayer.colors = nil
inlayer.backgroundColor = color.cgColor
}
}
}
open var indeterminate: Bool = false {
didSet {
if !indeterminate {
animating = false
}
sublayer.isHidden = indeterminate
inlayer.isHidden = !indeterminate
}
}
// MARK: - view lifecycle
override open func awakeFromNib() {
super.awakeFromNib()
//
wantsLayer = true
sublayer.anchorPoint = CGPoint.zero
layer!.backgroundColor = backgroundColor.cgColor
layer!.addSublayer(sublayer)
inlayer.anchorPoint = CGPoint.zero
inlayer.startPoint = CGPoint(x: 0, y: 0.5)
inlayer.endPoint = CGPoint(x: 1, y: 0.5)
inlayer.frame = bounds
layer!.addSublayer(inlayer)
inlayer.isHidden = true
updateColor()
updateLayerAnimated(false)
}
override open func layout() {
super.layout()
//
updateLayerAnimated(false)
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
inlayer.frame = bounds
CATransaction.commit()
}
// MARK: - progress value setters
open func append(_ progress: Double, animated: Bool) {
let value = progressValue + progress
setProgress(value, animated: animated)
}
open func setProgress(_ progress: Double, animated: Bool) {
// set new value
progressValue = progress > kRTProgressBarMaxProgressValue ? kRTProgressBarMaxProgressValue : progress
updateLayerAnimated(animated)
}
// MARK: - inner methods
fileprivate func updateColor() {
sublayer.backgroundColor = color.cgColor
let rightColor = animationColor ?? NSColor.clear
let inColor = animating ? rightColor : color
inlayer.colors = [color.cgColor, color.cgColor, inColor.cgColor, color.cgColor, color.cgColor]
}
fileprivate func updateLayerAnimated(_ animated: Bool) {
// synchronize
lock.lock()
defer {
lock.unlock()
}
// frame
let size = bounds.size
let value = progressValue / kRTProgressBarMaxProgressValue
let layerWidth = CGFloat(Double(size.width) * value)
if animated {
// add animation
let toValue = CGRect(origin: CGPoint.zero, size: CGSize(width: layerWidth, height: size.height))
let animation = CABasicAnimation(keyPath: "frame")
animation.duration = 0.3
animation.fromValue = NSValue(rect: sublayer.frame)
animation.toValue = NSValue(rect: toValue)
animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.linear)
sublayer.frame = toValue
sublayer.add(animation, forKey: "frame")
} else {
CATransaction.begin()
CATransaction.setValue(kCFBooleanTrue, forKey: kCATransactionDisableActions)
sublayer.frame = CGRect(origin: CGPoint.zero, size: CGSize(width: layerWidth, height: size.height))
CATransaction.commit()
}
}
}