forked from LoopKit/LoopKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChartContainerView.swift
More file actions
51 lines (42 loc) · 1.18 KB
/
ChartContainerView.swift
File metadata and controls
51 lines (42 loc) · 1.18 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
//
// ChartContainerView.swift
// LoopKitUI
//
// Created by Nate Racklyeft on 9/14/16.
// Copyright © 2016 Nathan Racklyeft. All rights reserved.
//
import UIKit
public class ChartContainerView: UIView {
override public func layoutSubviews() {
super.layoutSubviews()
if chartView == nil || chartView!.frame != bounds {
// 50 is the smallest height in which we should attempt to redraw a chart.
// Smaller sizes might be requested mid-animation, so ignore them.
if bounds.height > 50 {
chartView = chartGenerator?(bounds)
}
} else if chartView!.superview == nil {
addSubview(chartView!)
}
}
public func reloadChart() {
chartView = nil
setNeedsLayout()
}
public var chartGenerator: ((CGRect) -> UIView?)? {
didSet {
chartView = nil
setNeedsLayout()
}
}
private var chartView: UIView? {
didSet {
if let view = oldValue {
view.removeFromSuperview()
}
if let view = chartView {
self.addSubview(view)
}
}
}
}