-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimatedLabel.swift
139 lines (115 loc) · 3.9 KB
/
AnimatedLabel.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
import Foundation
import UIKit
enum CountingMethod {
case easeInOut, easeIn, easeOut, linear
}
enum AnimationDuration {
case laborious, plodding, strolling, brisk, instant
var value: Double {
switch self {
case .laborious: return 20.0
case .plodding: return 15.0
case .strolling: return 8.0
case .brisk: return 2.0
case .instant: return 0.0
}
}
}
enum DecimalPoints {
case zero, one, two, ridiculous
var format: String {
switch self {
case .zero: return "%.0f"
case .one: return "%.1f"
case .two: return "%.2f"
case .ridiculous: return "%f"
}
}
}
typealias OptionalCallback = (() -> Void)
typealias OptionalFormatBlock = (() -> String)
class AnimatedLabel: UILabel {
var completion: OptionalCallback?
var animationDuration: AnimationDuration = .brisk
var decimalPoints: DecimalPoints = .zero
var countingMethod: CountingMethod = .easeInOut
var customFormatBlock: OptionalFormatBlock?
fileprivate var currentValue: Float {
if progress >= totalTime { return destinationValue }
return startingValue + (update(t: Float(progress / totalTime)) * (destinationValue - startingValue))
}
fileprivate var rate: Float = 0
fileprivate var startingValue: Float = 0
fileprivate var destinationValue: Float = 0
fileprivate var progress: TimeInterval = 0
fileprivate var lastUpdate: TimeInterval = 0
fileprivate var totalTime: TimeInterval = 0
fileprivate var easingRate: Float = 0
fileprivate var timer: CADisplayLink?
func count(from: Float, to: Float, duration: TimeInterval? = nil) {
startingValue = from
destinationValue = to
timer?.invalidate()
timer = nil
if (duration == 0.0) {
// No animation
setTextValue(value: to)
completion?()
return
}
easingRate = 3.0
progress = 0.0
totalTime = duration ?? animationDuration.value
lastUpdate = Date.timeIntervalSinceReferenceDate
rate = 3.0
addDisplayLink()
}
func countFromCurrent(to: Float, duration: TimeInterval? = nil) {
count(from: currentValue, to: to, duration: duration ?? nil)
}
func countFromZero(to: Float, duration: TimeInterval? = nil) {
count(from: 0, to: to, duration: duration ?? nil)
}
func stop() {
timer?.invalidate()
timer = nil
progress = totalTime
completion?()
}
fileprivate func addDisplayLink() {
timer = CADisplayLink(target: self, selector: #selector(self.updateValue(timer:)))
timer?.add(to: .main, forMode: RunLoop.Mode.default)
timer?.add(to: .main, forMode: RunLoop.Mode.tracking)
}
fileprivate func update(t: Float) -> Float {
var t = t
switch countingMethod {
case .linear:
return t
case .easeIn:
return powf(t, rate)
case .easeInOut:
var sign: Float = 1
if Int(rate) % 2 == 0 { sign = -1 }
t *= 2
return t < 1 ? 0.5 * powf(t, rate) : (sign*0.5) * (powf(t-2, rate) + sign*2)
case .easeOut:
return 1.0-powf((1.0-t), rate);
}
}
@objc fileprivate func updateValue(timer: Timer) {
let now: TimeInterval = Date.timeIntervalSinceReferenceDate
progress += now - lastUpdate
lastUpdate = now
if progress >= totalTime {
self.timer?.invalidate()
self.timer = nil
progress = totalTime
}
setTextValue(value: currentValue)
if progress == totalTime { completion?() }
}
fileprivate func setTextValue(value: Float) {
text = String(format: customFormatBlock?() ?? decimalPoints.format, value)
}
}