M13CheckboxController.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // M13CheckboxController.swift
  3. // M13Checkbox
  4. //
  5. // Created by McQuilkin, Brandon on 3/18/16.
  6. // Copyright © 2016 Brandon McQuilkin. All rights reserved.
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. import UIKit
  14. internal class M13CheckboxController {
  15. //----------------------------
  16. // MARK: - Properties
  17. //----------------------------
  18. /// The path presets for the manager.
  19. var pathGenerator: M13CheckboxPathGenerator = M13CheckboxCheckPathGenerator()
  20. /// The animation presets for the manager.
  21. var animationGenerator: M13CheckboxAnimationGenerator = M13CheckboxAnimationGenerator()
  22. /// The current state of the checkbox.
  23. var state: M13Checkbox.CheckState = DefaultValues.checkState
  24. /// The current tint color.
  25. /// - Note: Subclasses should override didSet to update the layers when this value changes.
  26. var tintColor: UIColor = UIColor.black
  27. /// The secondary tint color.
  28. /// - Note: Subclasses should override didSet to update the layers when this value changes.
  29. var secondaryTintColor: UIColor? = UIColor.lightGray
  30. /// The secondary color of the mark.
  31. /// - Note: Subclasses should override didSet to update the layers when this value changes.
  32. var secondaryCheckmarkTintColor: UIColor? = UIColor.white
  33. /// Whether or not to hide the box.
  34. /// - Note: Subclasses should override didSet to update the layers when this value changes.
  35. var hideBox: Bool = false
  36. /// Whether or not to allow morphong between states.
  37. var enableMorphing: Bool = true
  38. // The type of mark to display.
  39. var markType: M13Checkbox.MarkType {
  40. get {
  41. return _markType
  42. }
  43. set {
  44. setMarkType(type: newValue, animated: false)
  45. }
  46. }
  47. private var _markType: M13Checkbox.MarkType = DefaultValues.markType
  48. func setMarkType(type: M13Checkbox.MarkType, animated: Bool) {
  49. guard type != _markType else {
  50. return
  51. }
  52. _setMarkType(type: type, animated: animated)
  53. _markType = type
  54. }
  55. private func _setMarkType(type: M13Checkbox.MarkType, animated: Bool) {
  56. var newPathGenerator: M13CheckboxPathGenerator
  57. switch type {
  58. case .checkmark:
  59. newPathGenerator = M13CheckboxCheckPathGenerator()
  60. case .radio:
  61. newPathGenerator = M13CheckboxRadioPathGenerator()
  62. case .addRemove:
  63. newPathGenerator = M13CheckboxAddRemovePathGenerator()
  64. case .disclosure:
  65. newPathGenerator = M13CheckboxDisclosurePathGenerator()
  66. }
  67. newPathGenerator.boxLineWidth = pathGenerator.boxLineWidth
  68. newPathGenerator.boxType = pathGenerator.boxType
  69. newPathGenerator.checkmarkLineWidth = pathGenerator.checkmarkLineWidth
  70. newPathGenerator.cornerRadius = pathGenerator.cornerRadius
  71. newPathGenerator.size = pathGenerator.size
  72. // Animate the change.
  73. if pathGenerator.pathForMark(state) != nil && animated {
  74. let previousState = state
  75. animate(state, toState: nil, completion: { [weak self] in
  76. self?.pathGenerator = newPathGenerator
  77. self?.resetLayersForState(previousState)
  78. if self?.pathGenerator.pathForMark(previousState) != nil {
  79. self?.animate(nil, toState: previousState)
  80. }
  81. })
  82. } else if newPathGenerator.pathForMark(state) != nil && animated {
  83. let previousState = state
  84. pathGenerator = newPathGenerator
  85. resetLayersForState(nil)
  86. animate(nil, toState: previousState)
  87. } else {
  88. pathGenerator = newPathGenerator
  89. resetLayersForState(state)
  90. }
  91. }
  92. //----------------------------
  93. // MARK: - Layers
  94. //----------------------------
  95. /// The layers to display in the checkbox. The top layer is the last layer in the array.
  96. var layersToDisplay: [CALayer] {
  97. return []
  98. }
  99. //----------------------------
  100. // MARK: - Animations
  101. //----------------------------
  102. /**
  103. Animates the layers between the two states.
  104. - parameter fromState: The previous state of the checkbox.
  105. - parameter toState: The new state of the checkbox.
  106. */
  107. func animate(_ fromState: M13Checkbox.CheckState?, toState: M13Checkbox.CheckState?, completion: (() -> Void)? = nil) {
  108. if let toState = toState {
  109. state = toState
  110. }
  111. }
  112. //----------------------------
  113. // MARK: - Layout
  114. //----------------------------
  115. /// Layout the layers.
  116. func layoutLayers() {
  117. }
  118. //----------------------------
  119. // MARK: - Display
  120. //----------------------------
  121. /**
  122. Reset the layers to be in the given state.
  123. - parameter state: The new state of the checkbox.
  124. */
  125. func resetLayersForState(_ state: M13Checkbox.CheckState?) {
  126. if let state = state {
  127. self.state = state
  128. }
  129. layoutLayers()
  130. }
  131. }