M13CheckboxSpiralController.swift 12 KB

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