M13CheckboxFillController.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // M13CheckboxFillController.swift
  3. // M13Checkbox
  4. //
  5. // Created by McQuilkin, Brandon on 3/30/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 M13CheckboxFillController: M13CheckboxController {
  15. //----------------------------
  16. // MARK: - Properties
  17. //----------------------------
  18. override var tintColor: UIColor {
  19. didSet {
  20. selectedBoxLayer.strokeColor = tintColor.cgColor
  21. selectedBoxLayer.fillColor = tintColor.cgColor
  22. }
  23. }
  24. override var secondaryTintColor: UIColor? {
  25. didSet {
  26. unselectedBoxLayer.strokeColor = secondaryTintColor?.cgColor
  27. }
  28. }
  29. override var secondaryCheckmarkTintColor: UIColor? {
  30. didSet {
  31. markLayer.strokeColor = secondaryCheckmarkTintColor?.cgColor
  32. }
  33. }
  34. override var hideBox: Bool {
  35. didSet {
  36. selectedBoxLayer.isHidden = hideBox
  37. unselectedBoxLayer.isHidden = hideBox
  38. }
  39. }
  40. override init() {
  41. // Disable som implicit animations.
  42. let newActions = [
  43. "opacity": NSNull(),
  44. "strokeEnd": NSNull(),
  45. "transform": NSNull(),
  46. "fillColor": NSNull(),
  47. "path": NSNull(),
  48. "lineWidth": NSNull()
  49. ]
  50. // Setup the unselected box layer
  51. unselectedBoxLayer.lineCap = .round
  52. unselectedBoxLayer.rasterizationScale = UIScreen.main.scale
  53. unselectedBoxLayer.shouldRasterize = true
  54. unselectedBoxLayer.actions = newActions
  55. unselectedBoxLayer.opacity = 1.0
  56. unselectedBoxLayer.strokeEnd = 1.0
  57. unselectedBoxLayer.transform = CATransform3DIdentity
  58. unselectedBoxLayer.fillColor = nil
  59. // Setup the selected box layer.
  60. selectedBoxLayer.lineCap = .round
  61. selectedBoxLayer.rasterizationScale = UIScreen.main.scale
  62. selectedBoxLayer.shouldRasterize = true
  63. selectedBoxLayer.actions = newActions
  64. selectedBoxLayer.transform = CATransform3DIdentity
  65. // Setup the checkmark layer.
  66. markLayer.lineCap = .round
  67. markLayer.lineJoin = .round
  68. markLayer.rasterizationScale = UIScreen.main.scale
  69. markLayer.shouldRasterize = true
  70. markLayer.actions = newActions
  71. markLayer.transform = CATransform3DIdentity
  72. markLayer.fillColor = nil
  73. }
  74. //----------------------------
  75. // MARK: - Layers
  76. //----------------------------
  77. let markLayer = CAShapeLayer()
  78. let selectedBoxLayer = CAShapeLayer()
  79. let unselectedBoxLayer = CAShapeLayer()
  80. override var layersToDisplay: [CALayer] {
  81. return [unselectedBoxLayer, selectedBoxLayer, markLayer]
  82. }
  83. //----------------------------
  84. // MARK: - Animations
  85. //----------------------------
  86. override func animate(_ fromState: M13Checkbox.CheckState?, toState: M13Checkbox.CheckState?, completion: (() -> Void)?) {
  87. super.animate(fromState, toState: toState)
  88. if pathGenerator.pathForMark(toState) == nil && pathGenerator.pathForMark(fromState) != nil {
  89. let wiggleAnimation = animationGenerator.fillAnimation(1, amplitude: 0.18, reverse: true)
  90. let opacityAnimation = animationGenerator.opacityAnimation(true)
  91. CATransaction.begin()
  92. CATransaction.setCompletionBlock({ () -> Void in
  93. self.resetLayersForState(toState)
  94. completion?()
  95. })
  96. selectedBoxLayer.add(wiggleAnimation, forKey: "transform")
  97. markLayer.add(opacityAnimation, forKey: "opacity")
  98. CATransaction.commit()
  99. } else if pathGenerator.pathForMark(toState) != nil && pathGenerator.pathForMark(fromState) == nil {
  100. markLayer.path = pathGenerator.pathForMark(toState)?.cgPath
  101. let wiggleAnimation = animationGenerator.fillAnimation(1, amplitude: 0.18, reverse: false)
  102. let opacityAnimation = animationGenerator.opacityAnimation(false)
  103. CATransaction.begin()
  104. CATransaction.setCompletionBlock({ () -> Void in
  105. self.resetLayersForState(toState)
  106. completion?()
  107. })
  108. selectedBoxLayer.add(wiggleAnimation, forKey: "transform")
  109. markLayer.add(opacityAnimation, forKey: "opacity")
  110. CATransaction.commit()
  111. } else {
  112. let fromPath = pathGenerator.pathForMark(fromState)
  113. let toPath = pathGenerator.pathForMark(toState)
  114. let morphAnimation = animationGenerator.morphAnimation(fromPath, toPath: toPath)
  115. CATransaction.begin()
  116. CATransaction.setCompletionBlock({ [unowned self] () -> Void in
  117. self.resetLayersForState(self.state)
  118. completion?()
  119. })
  120. markLayer.add(morphAnimation, forKey: "path")
  121. CATransaction.commit()
  122. }
  123. }
  124. //----------------------------
  125. // MARK: - Layout
  126. //----------------------------
  127. override func layoutLayers() {
  128. // Frames
  129. unselectedBoxLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  130. selectedBoxLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  131. markLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  132. // Paths
  133. unselectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  134. selectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  135. markLayer.path = pathGenerator.pathForMark(state)?.cgPath
  136. }
  137. //----------------------------
  138. // MARK: - Display
  139. //----------------------------
  140. override func resetLayersForState(_ state: M13Checkbox.CheckState?) {
  141. super.resetLayersForState(state)
  142. // Remove all remnant animations. They will interfere with each other if they are not removed before a new round of animations start.
  143. unselectedBoxLayer.removeAllAnimations()
  144. selectedBoxLayer.removeAllAnimations()
  145. markLayer.removeAllAnimations()
  146. // Set the properties for the final states of each necessary property of each layer.
  147. unselectedBoxLayer.strokeColor = secondaryTintColor?.cgColor
  148. unselectedBoxLayer.lineWidth = pathGenerator.boxLineWidth
  149. selectedBoxLayer.strokeColor = tintColor.cgColor
  150. selectedBoxLayer.fillColor = tintColor.cgColor
  151. selectedBoxLayer.lineWidth = pathGenerator.boxLineWidth
  152. markLayer.strokeColor = secondaryCheckmarkTintColor?.cgColor
  153. markLayer.lineWidth = pathGenerator.checkmarkLineWidth
  154. markLayer.fillColor = nil
  155. if pathGenerator.pathForMark(state) != nil {
  156. selectedBoxLayer.transform = CATransform3DMakeScale(1.0, 1.0, 1.0)
  157. markLayer.opacity = 1.0
  158. } else {
  159. selectedBoxLayer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
  160. markLayer.opacity = 0.0
  161. }
  162. // Paths
  163. unselectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  164. selectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  165. markLayer.path = pathGenerator.pathForMark(state)?.cgPath
  166. }
  167. }