ChartContainerView.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // ChartContainerView.swift
  3. // LoopKitUI
  4. //
  5. // Created by Nate Racklyeft on 9/14/16.
  6. // Copyright © 2016 Nathan Racklyeft. All rights reserved.
  7. //
  8. import UIKit
  9. public class ChartContainerView: UIView {
  10. override public func layoutSubviews() {
  11. super.layoutSubviews()
  12. if chartView == nil || chartView!.frame != bounds {
  13. // 50 is the smallest height in which we should attempt to redraw a chart.
  14. // Smaller sizes might be requested mid-animation, so ignore them.
  15. if bounds.height > 50 {
  16. chartView = chartGenerator?(bounds)
  17. }
  18. } else if chartView!.superview == nil {
  19. addSubview(chartView!)
  20. }
  21. }
  22. public func reloadChart() {
  23. chartView = nil
  24. setNeedsLayout()
  25. }
  26. public var chartGenerator: ((CGRect) -> UIView?)? {
  27. didSet {
  28. chartView = nil
  29. setNeedsLayout()
  30. }
  31. }
  32. private var chartView: UIView? {
  33. didSet {
  34. if let view = oldValue {
  35. view.removeFromSuperview()
  36. }
  37. if let view = chartView {
  38. self.addSubview(view)
  39. }
  40. }
  41. }
  42. }