M13CheckboxFlatController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // M13CheckboxFlatController.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 M13CheckboxFlatController: 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.transform = CATransform3DIdentity
  75. unselectedBoxLayer.fillColor = nil
  76. // Setup the selected box layer.
  77. selectedBoxLayer.lineCap = .round
  78. selectedBoxLayer.rasterizationScale = UIScreen.main.scale
  79. selectedBoxLayer.shouldRasterize = true
  80. selectedBoxLayer.actions = newActions
  81. selectedBoxLayer.fillColor = nil
  82. selectedBoxLayer.transform = CATransform3DIdentity
  83. // Setup the checkmark layer.
  84. markLayer.lineCap = .round
  85. markLayer.lineJoin = .round
  86. markLayer.rasterizationScale = UIScreen.main.scale
  87. markLayer.shouldRasterize = true
  88. markLayer.actions = newActions
  89. markLayer.transform = CATransform3DIdentity
  90. markLayer.fillColor = nil
  91. }
  92. //----------------------------
  93. // MARK: - Layers
  94. //----------------------------
  95. let markLayer = CAShapeLayer()
  96. let selectedBoxLayer = CAShapeLayer()
  97. let unselectedBoxLayer = CAShapeLayer()
  98. override var layersToDisplay: [CALayer] {
  99. return [unselectedBoxLayer, selectedBoxLayer, markLayer]
  100. }
  101. //----------------------------
  102. // MARK: - Animations
  103. //----------------------------
  104. override func animate(_ fromState: M13Checkbox.CheckState?, toState: M13Checkbox.CheckState?, completion: (() -> Void)?) {
  105. super.animate(fromState, toState: toState)
  106. if pathGenerator.pathForMark(toState) == nil && pathGenerator.pathForMark(fromState) != nil {
  107. let morphAnimation = animationGenerator.morphAnimation(pathGenerator.pathForMark(), toPath: pathGenerator.pathForMixedMark())
  108. morphAnimation.timingFunction = CAMediaTimingFunction(name: .easeIn)
  109. let opacityAnimation = animationGenerator.opacityAnimation(true)
  110. let quickOpacityAnimation = animationGenerator.quickOpacityAnimation(true)
  111. quickOpacityAnimation.duration = quickOpacityAnimation.duration * 4.0
  112. morphAnimation.duration = morphAnimation.duration - quickOpacityAnimation.duration
  113. quickOpacityAnimation.beginTime = CACurrentMediaTime() + morphAnimation.duration
  114. CATransaction.begin()
  115. CATransaction.setCompletionBlock({ () -> Void in
  116. self.resetLayersForState(toState)
  117. completion?()
  118. })
  119. selectedBoxLayer.add(opacityAnimation, forKey: "opacity")
  120. if fromState != .mixed {
  121. markLayer.add(morphAnimation, forKey: "path")
  122. }
  123. markLayer.add(quickOpacityAnimation, forKey: "opacity")
  124. CATransaction.commit()
  125. } else if pathGenerator.pathForMark(toState) != nil && pathGenerator.pathForMark(fromState) == nil {
  126. markLayer.path = pathGenerator.pathForMixedMark()?.cgPath
  127. let morphAnimation = animationGenerator.morphAnimation(pathGenerator.pathForMixedMark(), toPath: pathGenerator.pathForMark())
  128. morphAnimation.timingFunction = CAMediaTimingFunction(name: .easeOut)
  129. let opacityAnimation = animationGenerator.opacityAnimation(false)
  130. let quickOpacityAnimation = animationGenerator.quickOpacityAnimation(false)
  131. quickOpacityAnimation.duration = quickOpacityAnimation.duration * 4.0
  132. morphAnimation.beginTime = CACurrentMediaTime() + quickOpacityAnimation.duration
  133. morphAnimation.duration = morphAnimation.duration - quickOpacityAnimation.duration
  134. CATransaction.begin()
  135. CATransaction.setCompletionBlock({ () -> Void in
  136. self.resetLayersForState(toState)
  137. completion?()
  138. })
  139. selectedBoxLayer.add(opacityAnimation, forKey: "opacity")
  140. if toState != .mixed {
  141. markLayer.add(morphAnimation, forKey: "path")
  142. }
  143. markLayer.add(quickOpacityAnimation, forKey: "opacity")
  144. CATransaction.commit()
  145. } else {
  146. let fromPath = pathGenerator.pathForMark(fromState)
  147. let toPath = pathGenerator.pathForMark(toState)
  148. let morphAnimation = animationGenerator.morphAnimation(fromPath, toPath: toPath)
  149. CATransaction.begin()
  150. CATransaction.setCompletionBlock({ [unowned self] () -> Void in
  151. self.resetLayersForState(self.state)
  152. completion?()
  153. })
  154. markLayer.add(morphAnimation, forKey: "path")
  155. CATransaction.commit()
  156. }
  157. }
  158. //----------------------------
  159. // MARK: - Layout
  160. //----------------------------
  161. override func layoutLayers() {
  162. // Frames
  163. unselectedBoxLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  164. selectedBoxLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  165. markLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  166. // Paths
  167. unselectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  168. selectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  169. markLayer.path = pathGenerator.pathForMark(state)?.cgPath
  170. }
  171. //----------------------------
  172. // MARK: - Display
  173. //----------------------------
  174. override func resetLayersForState(_ state: M13Checkbox.CheckState?) {
  175. super.resetLayersForState(state)
  176. // Remove all remnant animations. They will interfere with each other if they are not removed before a new round of animations start.
  177. unselectedBoxLayer.removeAllAnimations()
  178. selectedBoxLayer.removeAllAnimations()
  179. markLayer.removeAllAnimations()
  180. // Set the properties for the final states of each necessary property of each layer.
  181. unselectedBoxLayer.strokeColor = secondaryTintColor?.cgColor
  182. unselectedBoxLayer.lineWidth = pathGenerator.boxLineWidth
  183. selectedBoxLayer.strokeColor = tintColor.cgColor
  184. selectedBoxLayer.lineWidth = pathGenerator.boxLineWidth
  185. if style == .stroke {
  186. selectedBoxLayer.fillColor = nil
  187. markLayer.strokeColor = tintColor.cgColor
  188. if markType != .radio {
  189. markLayer.fillColor = nil
  190. } else {
  191. markLayer.fillColor = tintColor.cgColor
  192. }
  193. } else {
  194. selectedBoxLayer.fillColor = tintColor.cgColor
  195. markLayer.strokeColor = secondaryCheckmarkTintColor?.cgColor
  196. }
  197. markLayer.lineWidth = pathGenerator.checkmarkLineWidth
  198. if pathGenerator.pathForMark(state) != nil {
  199. selectedBoxLayer.opacity = 1.0
  200. markLayer.opacity = 1.0
  201. } else {
  202. selectedBoxLayer.opacity = 0.0
  203. markLayer.opacity = 0.0
  204. }
  205. // Paths
  206. unselectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  207. selectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  208. markLayer.path = pathGenerator.pathForMark(state)?.cgPath
  209. }
  210. }