M13CheckboxFadeController.swift 9.0 KB

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