-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathLabelPool.swift
More file actions
53 lines (41 loc) · 1.33 KB
/
LabelPool.swift
File metadata and controls
53 lines (41 loc) · 1.33 KB
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
import UIKit
internal class LabelPool {
var labels = [UILabel]()
var relations = [Int : Int]()
var unused = [Int]()
func deactivateLabel(forPointIndex pointIndex: Int){
if let unusedLabelIndex = relations[pointIndex] {
unused.append(unusedLabelIndex)
}
relations[pointIndex] = nil
}
@discardableResult
func activateLabel(forPointIndex pointIndex: Int) -> UILabel {
var label: UILabel
if(unused.count >= 1) {
let unusedLabelIndex = unused.first!
unused.removeFirst()
label = labels[unusedLabelIndex]
relations[pointIndex] = unusedLabelIndex
}
else {
label = UILabel()
labels.append(label)
let newLabelIndex = labels.firstIndex(of: label)!
relations[pointIndex] = newLabelIndex
}
return label
}
var activeLabels: [UILabel] {
get {
var currentlyActive = [UILabel]()
let numberOfLabels = labels.count
for i in 0 ..< numberOfLabels {
if(!unused.contains(i)) {
currentlyActive.append(labels[i])
}
}
return currentlyActive
}
}
}