M13CheckboxDotController.swift 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // M13CheckboxDotController.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 M13CheckboxDotController: 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 scaleAnimation = animationGenerator.fillAnimation(1, amplitude: 0.18, reverse: true)
  108. let opacityAnimation = animationGenerator.opacityAnimation(true)
  109. CATransaction.begin()
  110. CATransaction.setCompletionBlock({ () -> Void in
  111. self.resetLayersForState(toState)
  112. completion?()
  113. })
  114. if style == .stroke {
  115. unselectedBoxLayer.opacity = 0.0
  116. let quickOpacityAnimation = animationGenerator.quickOpacityAnimation(false)
  117. quickOpacityAnimation.beginTime = CACurrentMediaTime() + scaleAnimation.duration - quickOpacityAnimation.duration
  118. unselectedBoxLayer.add(quickOpacityAnimation, forKey: "opacity")
  119. }
  120. selectedBoxLayer.add(scaleAnimation, forKey: "transform")
  121. markLayer.add(opacityAnimation, forKey: "opacity")
  122. CATransaction.commit()
  123. } else if pathGenerator.pathForMark(toState) != nil && pathGenerator.pathForMark(fromState) == nil {
  124. markLayer.path = pathGenerator.pathForMark(toState)?.cgPath
  125. let scaleAnimation = animationGenerator.fillAnimation(1, amplitude: 0.18, reverse: false)
  126. let opacityAnimation = animationGenerator.opacityAnimation(false)
  127. CATransaction.begin()
  128. CATransaction.setCompletionBlock({ () -> Void in
  129. self.resetLayersForState(toState)
  130. completion?()
  131. })
  132. if style == .stroke {
  133. let quickOpacityAnimation = animationGenerator.quickOpacityAnimation(true)
  134. quickOpacityAnimation.beginTime = CACurrentMediaTime()
  135. unselectedBoxLayer.add(quickOpacityAnimation, forKey: "opacity")
  136. }
  137. selectedBoxLayer.add(scaleAnimation, forKey: "transform")
  138. markLayer.add(opacityAnimation, forKey: "opacity")
  139. CATransaction.commit()
  140. } else {
  141. let fromPath = pathGenerator.pathForMark(fromState)
  142. let toPath = pathGenerator.pathForMark(toState)
  143. let morphAnimation = animationGenerator.morphAnimation(fromPath, toPath: toPath)
  144. CATransaction.begin()
  145. CATransaction.setCompletionBlock({ [unowned self] () -> Void in
  146. self.resetLayersForState(self.state)
  147. completion?()
  148. })
  149. markLayer.add(morphAnimation, forKey: "path")
  150. CATransaction.commit()
  151. }
  152. }
  153. //----------------------------
  154. // MARK: - Layout
  155. //----------------------------
  156. override func layoutLayers() {
  157. // Frames
  158. unselectedBoxLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  159. selectedBoxLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  160. markLayer.frame = CGRect(x: 0.0, y: 0.0, width: pathGenerator.size, height: pathGenerator.size)
  161. // Paths
  162. unselectedBoxLayer.path = pathGenerator.pathForDot()?.cgPath
  163. selectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  164. markLayer.path = pathGenerator.pathForMark(state)?.cgPath
  165. }
  166. //----------------------------
  167. // MARK: - Display
  168. //----------------------------
  169. override func resetLayersForState(_ state: M13Checkbox.CheckState?) {
  170. super.resetLayersForState(state)
  171. // Remove all remnant animations. They will interfere with each other if they are not removed before a new round of animations start.
  172. unselectedBoxLayer.removeAllAnimations()
  173. selectedBoxLayer.removeAllAnimations()
  174. markLayer.removeAllAnimations()
  175. // Set the properties for the final states of each necessary property of each layer.
  176. unselectedBoxLayer.strokeColor = secondaryTintColor?.cgColor
  177. unselectedBoxLayer.lineWidth = pathGenerator.boxLineWidth
  178. selectedBoxLayer.strokeColor = tintColor.cgColor
  179. selectedBoxLayer.lineWidth = pathGenerator.boxLineWidth
  180. if style == .stroke {
  181. selectedBoxLayer.fillColor = nil
  182. markLayer.strokeColor = tintColor.cgColor
  183. if markType != .radio {
  184. markLayer.fillColor = nil
  185. } else {
  186. markLayer.fillColor = tintColor.cgColor
  187. }
  188. } else {
  189. selectedBoxLayer.fillColor = tintColor.cgColor
  190. markLayer.strokeColor = secondaryCheckmarkTintColor?.cgColor
  191. }
  192. markLayer.lineWidth = pathGenerator.checkmarkLineWidth
  193. if pathGenerator.pathForMark(state) != nil {
  194. unselectedBoxLayer.opacity = 0.0
  195. selectedBoxLayer.transform = CATransform3DIdentity
  196. markLayer.opacity = 1.0
  197. } else {
  198. unselectedBoxLayer.opacity = 1.0
  199. selectedBoxLayer.transform = CATransform3DMakeScale(0.0, 0.0, 0.0)
  200. markLayer.opacity = 0.0
  201. }
  202. // Paths
  203. unselectedBoxLayer.path = pathGenerator.pathForDot()?.cgPath
  204. selectedBoxLayer.path = pathGenerator.pathForBox()?.cgPath
  205. markLayer.path = pathGenerator.pathForMark(state)?.cgPath
  206. }
  207. }