M13CheckboxPathGenerator.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //
  2. // M13CheckboxPathGenerator.swift
  3. // M13Checkbox
  4. //
  5. // Created by McQuilkin, Brandon on 10/6/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. /// The base class that generates the paths for the different mark types.
  15. internal class M13CheckboxPathGenerator {
  16. //----------------------------
  17. // MARK: - Properties
  18. //----------------------------
  19. /// The maximum width or height the path will be generated with.
  20. var size: CGFloat = 0.0
  21. /// The line width of the created checkmark.
  22. var checkmarkLineWidth: CGFloat = 1.0
  23. /// The line width of the created box.
  24. var boxLineWidth: CGFloat = 1.0
  25. /// The corner radius of the box.
  26. var cornerRadius: CGFloat = 3.0
  27. /// The box type to create.
  28. var boxType: M13Checkbox.BoxType = DefaultValues.boxType
  29. //----------------------------
  30. // MARK: - Box Paths
  31. //----------------------------
  32. /**
  33. Creates a path object for the box.
  34. - returns: A `UIBezierPath` representing the box.
  35. */
  36. final func pathForBox() -> UIBezierPath? {
  37. switch boxType {
  38. case .circle:
  39. return pathForCircle()
  40. case .square:
  41. return pathForRoundedRect()
  42. }
  43. }
  44. /**
  45. Creates a circular path for the box. The path starts at the top center point of the box.
  46. - returns: A `UIBezierPath` representing the box.
  47. */
  48. func pathForCircle() -> UIBezierPath? {
  49. let radius = (size - boxLineWidth) / 2.0
  50. // Create a circle that starts in the top right hand corner.
  51. return UIBezierPath(arcCenter: CGPoint(x: size / 2.0, y: size / 2.0),
  52. radius: radius,
  53. startAngle: -(CGFloat.pi / 2),
  54. endAngle: CGFloat((2 * Double.pi) - (Double.pi / 2)),
  55. clockwise: true)
  56. }
  57. /**
  58. Creates a rounded rect path for the box. The path starts at the top center point of the box.
  59. - returns: A `UIBezierPath` representing the box.
  60. */
  61. func pathForRoundedRect() -> UIBezierPath? {
  62. let path = UIBezierPath()
  63. let lineOffset: CGFloat = boxLineWidth / 2.0
  64. let trX: CGFloat = size - lineOffset - cornerRadius
  65. let trY: CGFloat = 0.0 + lineOffset + cornerRadius
  66. let tr = CGPoint(x: trX, y: trY)
  67. let brX: CGFloat = size - lineOffset - cornerRadius
  68. let brY: CGFloat = size - lineOffset - cornerRadius
  69. let br = CGPoint(x: brX, y: brY)
  70. let blX: CGFloat = 0.0 + lineOffset + cornerRadius
  71. let blY: CGFloat = size - lineOffset - cornerRadius
  72. let bl = CGPoint(x: blX, y: blY)
  73. let tlX: CGFloat = 0.0 + lineOffset + cornerRadius
  74. let tlY: CGFloat = 0.0 + lineOffset + cornerRadius
  75. let tl = CGPoint(x: tlX, y: tlY)
  76. path.move(to: CGPoint(x: (tl.x + tr.x) / 2.0, y: ((tl.y + tr.y) / 2.0) - cornerRadius))
  77. // Top side.
  78. let trYCr: CGFloat = tr.y - cornerRadius
  79. path.addLine(to: CGPoint(x: tr.x, y: trYCr))
  80. // Right arc
  81. if cornerRadius != 0 {
  82. path.addArc(withCenter: tr,
  83. radius: cornerRadius,
  84. startAngle: -(CGFloat.pi / 2),
  85. endAngle: 0.0,
  86. clockwise: true)
  87. }
  88. // Right side.
  89. let brXCr: CGFloat = br.x + cornerRadius
  90. path.addLine(to: CGPoint(x: brXCr, y: br.y))
  91. // Bottom right arc.
  92. if cornerRadius != 0 {
  93. path.addArc(withCenter: br,
  94. radius: cornerRadius,
  95. startAngle: 0.0,
  96. endAngle: CGFloat.pi / 2,
  97. clockwise: true)
  98. }
  99. // Bottom side.
  100. let blYCr: CGFloat = bl.y + cornerRadius
  101. path.addLine(to: CGPoint(x: bl.x , y: blYCr))
  102. // Bottom left arc.
  103. if cornerRadius != 0 {
  104. path.addArc(withCenter: bl,
  105. radius: cornerRadius,
  106. startAngle: CGFloat.pi / 2,
  107. endAngle: CGFloat.pi,
  108. clockwise: true)
  109. }
  110. // Left side.
  111. let tlXCr: CGFloat = tl.x - cornerRadius
  112. path.addLine(to: CGPoint(x: tlXCr, y: tl.y))
  113. // Top left arc.
  114. if cornerRadius != 0 {
  115. path.addArc(withCenter: tl,
  116. radius: cornerRadius,
  117. startAngle: CGFloat.pi,
  118. endAngle: CGFloat(Double.pi + (Double.pi / 2)),
  119. clockwise: true)
  120. }
  121. path.close()
  122. return path
  123. }
  124. /**
  125. Creates a small dot for the box.
  126. - returns: A `UIBezierPath` representing the dot.
  127. */
  128. func pathForDot() -> UIBezierPath? {
  129. let boxPath = pathForBox()
  130. let scale: CGFloat = 1.0 / 20.0
  131. boxPath?.apply(CGAffineTransform(scaleX: scale, y: scale))
  132. boxPath?.apply(CGAffineTransform(translationX: (size - (size * scale)) / 2.0, y: (size - (size * scale)) / 2.0))
  133. return boxPath
  134. }
  135. //----------------------------
  136. // MARK: - Check Generation
  137. //----------------------------
  138. /**
  139. Generates the path for the mark for the given state.
  140. - parameter state: The state to generate the mark path for.
  141. - returns: A `UIBezierPath` representing the mark.
  142. */
  143. final func pathForMark(_ state: M13Checkbox.CheckState?) -> UIBezierPath? {
  144. guard let state = state else {
  145. return nil
  146. }
  147. switch state {
  148. case .unchecked:
  149. return pathForUnselectedMark()
  150. case .checked:
  151. return pathForMark()
  152. case .mixed:
  153. return pathForMixedMark()
  154. }
  155. }
  156. /**
  157. Generates the path for the long mark for the given state used in the spiral animation.
  158. - parameter state: The state to generate the long mark path for.
  159. - returns: A `UIBezierPath` representing the long mark.
  160. */
  161. final func pathForLongMark(_ state: M13Checkbox.CheckState?) -> UIBezierPath? {
  162. guard let state = state else {
  163. return nil
  164. }
  165. switch state {
  166. case .unchecked:
  167. return pathForLongUnselectedMark()
  168. case .checked:
  169. return pathForLongMark()
  170. case .mixed:
  171. return pathForLongMixedMark()
  172. }
  173. }
  174. /**
  175. Creates a path object for the mark.
  176. - returns: A `UIBezierPath` representing the mark.
  177. */
  178. func pathForMark() -> UIBezierPath? {
  179. return nil
  180. }
  181. /**
  182. Creates a path object for the long mark.
  183. - returns: A `UIBezierPath` representing the long mark.
  184. */
  185. func pathForLongMark() -> UIBezierPath? {
  186. return nil
  187. }
  188. /**
  189. Creates a path object for the mixed mark.
  190. - returns: A `UIBezierPath` representing the mixed mark.
  191. */
  192. func pathForMixedMark() -> UIBezierPath? {
  193. return nil
  194. }
  195. /**
  196. Creates a path object for the long mixed mark.
  197. - returns: A `UIBezierPath` representing the long mixed mark.
  198. */
  199. func pathForLongMixedMark() -> UIBezierPath? {
  200. return nil
  201. }
  202. /**
  203. Creates a path object for the unselected mark.
  204. - returns: A `UIBezierPath` representing the unselected mark.
  205. */
  206. func pathForUnselectedMark() -> UIBezierPath? {
  207. return nil
  208. }
  209. /**
  210. Creates a path object for the long unselected mark.
  211. - returns: A `UIBezierPath` representing the long unselected mark.
  212. */
  213. func pathForLongUnselectedMark() -> UIBezierPath? {
  214. return nil
  215. }
  216. }