M13CheckboxAnimationGenerator.swift 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // M13CheckboxAnimationGenerator.swift
  3. // M13Checkbox
  4. //
  5. // Created by McQuilkin, Brandon on 3/27/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 M13CheckboxAnimationGenerator {
  15. //----------------------------
  16. // MARK: - Properties
  17. //----------------------------
  18. // The duration of animations that are generated by the animation manager.
  19. var animationDuration: TimeInterval = 0.3
  20. // The frame rate for certian keyframe animations.
  21. fileprivate var frameRate: CGFloat = 60.0
  22. //----------------------------
  23. // MARK: - Quick Animations
  24. //----------------------------
  25. final func quickAnimation(_ key: String, reverse: Bool) -> CABasicAnimation {
  26. let animation = CABasicAnimation(keyPath: key)
  27. // Set the start and end.
  28. if !reverse {
  29. animation.fromValue = 0.0
  30. animation.toValue = 1.0
  31. animation.timingFunction = CAMediaTimingFunction(name: .easeIn)
  32. } else {
  33. animation.fromValue = 1.0
  34. animation.toValue = 0.0
  35. animation.beginTime = CACurrentMediaTime() + (animationDuration * 0.9)
  36. animation.timingFunction = CAMediaTimingFunction(name: .easeOut)
  37. }
  38. // Set animation properties.
  39. animation.duration = animationDuration / 10.0
  40. animation.isRemovedOnCompletion = false
  41. animation.fillMode = CAMediaTimingFillMode.forwards
  42. return animation
  43. }
  44. /**
  45. Creates an animation that either quickly fades a layer in or out.
  46. - note: Mainly used to smooth out the start and end of various animations.
  47. - parameter reverse: The direction of the animation.
  48. - returns: A `CABasicAnimation` that animates the opacity property.
  49. */
  50. final func quickOpacityAnimation(_ reverse: Bool) -> CABasicAnimation {
  51. return quickAnimation("opacity", reverse: reverse)
  52. }
  53. /**
  54. Creates an animation that either quickly changes the line width of a layer from 0% to 100%.
  55. - note: Mainly used to smooth out the start and end of various animations.
  56. - parameter reverse: The direction of the animation.
  57. - returns: A `CABasicAnimation` that animates the opacity property.
  58. */
  59. final func quickLineWidthAnimation(_ width: CGFloat, reverse: Bool) -> CABasicAnimation {
  60. let animation = quickAnimation("lineWidth", reverse: reverse)
  61. // Set the start and end.
  62. if !reverse {
  63. animation.toValue = width
  64. } else {
  65. animation.fromValue = width
  66. }
  67. return animation
  68. }
  69. //----------------------------
  70. // MARK: - Animation Component Generation
  71. //----------------------------
  72. final func animation(_ key: String, reverse: Bool) -> CABasicAnimation {
  73. let animation = CABasicAnimation(keyPath: key)
  74. // Set the start and end.
  75. if !reverse {
  76. animation.fromValue = 0.0
  77. animation.toValue = 1.0
  78. } else {
  79. animation.fromValue = 1.0
  80. animation.toValue = 0.0
  81. }
  82. // Set animation properties.
  83. animation.duration = animationDuration
  84. animation.isRemovedOnCompletion = false
  85. animation.fillMode = CAMediaTimingFillMode.forwards
  86. animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  87. return animation
  88. }
  89. /**
  90. Creates an animation that animates the stroke property.
  91. - parameter reverse: The direction of the animation.
  92. - returns: A `CABasicAnimation` that animates the stroke property.
  93. */
  94. final func strokeAnimation(_ reverse: Bool) -> CABasicAnimation {
  95. return animation("strokeEnd", reverse: reverse)
  96. }
  97. /**
  98. Creates an animation that animates the opacity property.
  99. - parameter reverse: The direction of the animation.
  100. - returns: A `CABasicAnimation` that animates the opacity property.
  101. */
  102. final func opacityAnimation(_ reverse: Bool) -> CABasicAnimation {
  103. return animation("opacity", reverse: reverse)
  104. }
  105. /**
  106. Creates an animation that animates between two `UIBezierPath`s.
  107. - parameter fromPath: The start path.
  108. - parameter toPath: The end path.
  109. - returns: A `CABasicAnimation` that animates a path between the `fromPath` and `toPath`.
  110. */
  111. final func morphAnimation(_ fromPath: UIBezierPath?, toPath: UIBezierPath?) -> CABasicAnimation {
  112. let animation = CABasicAnimation(keyPath: "path")
  113. // Set the start and end.
  114. animation.fromValue = fromPath?.cgPath
  115. animation.toValue = toPath?.cgPath
  116. // Set animation properties.
  117. animation.duration = animationDuration
  118. animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  119. animation.fillMode = CAMediaTimingFillMode.forwards
  120. animation.isRemovedOnCompletion = false
  121. return animation
  122. }
  123. /**
  124. Creates an animation that animates between a filled an unfilled box.
  125. - parameter numberOfBounces: The number of bounces in the animation.
  126. - parameter amplitude: The distance of the bounce.
  127. - parameter reverse: The direction of the animation.
  128. - returns: A `CAKeyframeAnimation` that animates a change in fill.
  129. */
  130. final func fillAnimation(_ numberOfBounces: Int, amplitude: CGFloat, reverse: Bool) -> CAKeyframeAnimation {
  131. var values = [CATransform3D]()
  132. var keyTimes = [Float]()
  133. // Add the start scale
  134. if !reverse {
  135. values.append(CATransform3DMakeScale(0.0, 0.0, 0.0))
  136. } else {
  137. values.append(CATransform3DMakeScale(1.0, 1.0, 1.0))
  138. }
  139. keyTimes.append(0.0)
  140. // Add the bounces.
  141. if numberOfBounces > 0 {
  142. for i in 1...numberOfBounces {
  143. let scale = i % 2 == 1 ? (1.0 + (amplitude / CGFloat(i))) : (1.0 - (amplitude / CGFloat(i)))
  144. let time = (Float(i) * 1.0) / Float(numberOfBounces + 1)
  145. values.append(CATransform3DMakeScale(scale, scale, scale))
  146. keyTimes.append(time)
  147. }
  148. }
  149. // Add the end scale.
  150. if !reverse {
  151. values.append(CATransform3DMakeScale(1.0, 1.0, 1.0))
  152. } else {
  153. values.append(CATransform3DMakeScale(0.0001, 0.0001, 0.0001))
  154. }
  155. keyTimes.append(1.0)
  156. // Create the animation.
  157. let animation = CAKeyframeAnimation(keyPath: "transform")
  158. animation.values = values.map({ NSValue(caTransform3D: $0) })
  159. animation.keyTimes = keyTimes.map({ NSNumber(value: $0 as Float) })
  160. animation.isRemovedOnCompletion = false
  161. animation.fillMode = CAMediaTimingFillMode.forwards
  162. animation.duration = animationDuration
  163. animation.timingFunction = CAMediaTimingFunction(name: .easeInEaseOut)
  164. return animation
  165. }
  166. }