ViewController.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // ViewController.swift
  3. // MKRingProgressViewExample
  4. //
  5. // Created by Max Konovalov on 20/10/15.
  6. // Copyright © 2015 Max Konovalov. All rights reserved.
  7. //
  8. import UIKit
  9. class ViewController: UIViewController {
  10. @IBOutlet weak var groupContainerView: UIView!
  11. @IBOutlet weak var progressGroup: RingProgressGroupView!
  12. @IBOutlet weak var iconsHeightConstraint: NSLayoutConstraint!
  13. var buttons = [RingProgressGroupButton]()
  14. var selectedIndex = 0
  15. override func viewDidLoad() {
  16. super.viewDidLoad()
  17. let containerView = UIView(frame: navigationController!.navigationBar.bounds)
  18. navigationController!.navigationBar.addSubview(containerView)
  19. // These are optional and only serve to improve accessibility
  20. progressGroup.ring1.accessibilityLabel = NSLocalizedString("Move", comment: "")
  21. progressGroup.ring2.accessibilityLabel = NSLocalizedString("Exercise", comment: "")
  22. progressGroup.ring3.accessibilityLabel = NSLocalizedString("Stand", comment: "")
  23. let n = 7
  24. for i in 0..<n {
  25. let w = (containerView.bounds.width - 16) / CGFloat(n)
  26. let h = containerView.bounds.height
  27. let button = RingProgressGroupButton(frame: CGRect(x: 8 + CGFloat(i) * w, y: 0, width: w, height: h))
  28. button.contentView.ringWidth = 4.5
  29. button.contentView.ringSpacing = 1
  30. button.contentView.ring1StartColor = progressGroup.ring1StartColor
  31. button.contentView.ring1EndColor = progressGroup.ring1EndColor
  32. button.contentView.ring2StartColor = progressGroup.ring2StartColor
  33. button.contentView.ring2EndColor = progressGroup.ring2EndColor
  34. button.contentView.ring3StartColor = progressGroup.ring3StartColor
  35. button.contentView.ring3EndColor = progressGroup.ring3EndColor
  36. containerView.addSubview(button)
  37. buttons.append(button)
  38. button.addTarget(self, action: #selector(ViewController.buttonTapped(_:)), for: .touchUpInside)
  39. }
  40. buttons[0].isSelected = true
  41. updateButtonsProgress()
  42. }
  43. override func viewDidAppear(_ animated: Bool) {
  44. super.viewDidAppear(animated)
  45. updateMainGroupProgress(delay: 0.5)
  46. }
  47. override func viewDidLayoutSubviews() {
  48. super.viewDidLayoutSubviews()
  49. iconsHeightConstraint.constant = progressGroup.ringWidth * 3 + progressGroup.ringSpacing * 2
  50. }
  51. @objc func buttonTapped(_ sender: RingProgressGroupButton) {
  52. let newIndex = buttons.firstIndex(of: sender) ?? 0
  53. if newIndex == selectedIndex {
  54. return
  55. }
  56. let dx = (newIndex > selectedIndex) ? -self.view.frame.width : self.view.frame.width
  57. buttons[selectedIndex].isSelected = false
  58. sender.isSelected = true
  59. selectedIndex = newIndex
  60. UIView.animate(withDuration: 0.2, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: [], animations: { () -> Void in
  61. self.groupContainerView.transform = CGAffineTransform(translationX: dx, y: 0)
  62. }) { (_) -> Void in
  63. self.groupContainerView.transform = CGAffineTransform(translationX: -dx, y: 0)
  64. self.progressGroup.ring1.progress = 0.0
  65. self.progressGroup.ring2.progress = 0.0
  66. self.progressGroup.ring3.progress = 0.0
  67. UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 0.9, initialSpringVelocity: 0.0, options: [], animations: { () -> Void in
  68. self.groupContainerView.transform = CGAffineTransform.identity
  69. }, completion: { (_) -> Void in
  70. self.updateMainGroupProgress()
  71. })
  72. }
  73. }
  74. private func updateButtonsProgress() {
  75. UIView.animate(withDuration: 0.5) {
  76. for button in self.buttons {
  77. button.contentView.ring1.progress = Double(arc4random() % 200) / 100.0
  78. button.contentView.ring2.progress = Double(arc4random() % 200) / 100.0
  79. button.contentView.ring3.progress = Double(arc4random() % 200) / 100.0
  80. }
  81. }
  82. }
  83. private func updateMainGroupProgress(delay: TimeInterval = 0.0) {
  84. let selectedGroup = buttons[selectedIndex]
  85. UIView.animate(withDuration: 1.0, delay: delay, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: [], animations: {
  86. self.progressGroup.ring1.progress = selectedGroup.contentView.ring1.progress
  87. self.progressGroup.ring2.progress = selectedGroup.contentView.ring2.progress
  88. self.progressGroup.ring3.progress = selectedGroup.contentView.ring3.progress
  89. }, completion: nil)
  90. }
  91. @IBAction func randomize(_ sender: AnyObject? = nil) {
  92. updateButtonsProgress()
  93. updateMainGroupProgress()
  94. }
  95. }