lottie-ios 3.2.3 → 3.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (434) hide show
  1. package/.github/actions/setup/action.yml +32 -0
  2. package/.github/issue_template.md +6 -23
  3. package/.github/workflows/main.yml +98 -0
  4. package/.github/workflows/stale_issues.yml +17 -0
  5. package/.spi.yml +6 -0
  6. package/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +7 -0
  7. package/.swiftpm/xcode/package.xcworkspace/xcuserdata/cal.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  8. package/.swiftpm/xcode/package.xcworkspace/xcuserdata/calstephens.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  9. package/.swiftpm/xcode/xcuserdata/{brandonwithrow.xcuserdatad → cal.xcuserdatad}/xcschemes/xcschememanagement.plist +25 -15
  10. package/.swiftpm/xcode/xcuserdata/calstephens.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  11. package/Gemfile +4 -0
  12. package/Gemfile.lock +102 -0
  13. package/Lottie.xcodeproj/project.pbxproj +2011 -1972
  14. package/Lottie.xcodeproj/project.xcworkspace/contents.xcworkspacedata +1 -1
  15. package/Lottie.xcodeproj/project.xcworkspace/xcuserdata/cal.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  16. package/Lottie.xcodeproj/xcshareddata/xcschemes/{Lottie_iOS.xcscheme → Lottie (iOS).xcscheme } +15 -18
  17. package/Lottie.xcodeproj/xcshareddata/xcschemes/{Lottie_tvOS.xcscheme → Lottie (macOS).xcscheme } +5 -18
  18. package/Lottie.xcodeproj/xcshareddata/xcschemes/{Lottie_macOS.xcscheme → Lottie (tvOS).xcscheme } +5 -18
  19. package/Lottie.xcodeproj/xcuserdata/cal.xcuserdatad/xcschemes/xcschememanagement.plist +37 -0
  20. package/Lottie.xcodeproj/xcuserdata/calstephens.xcuserdatad/xcschemes/xcschememanagement.plist +24 -0
  21. package/Lottie.xcworkspace/contents.xcworkspacedata +10 -0
  22. package/Lottie.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  23. package/Lottie.xcworkspace/xcshareddata/swiftpm/Package.resolved +34 -0
  24. package/Lottie.xcworkspace/xcuserdata/cal.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  25. package/{Lottie.xcodeproj/xcuserdata/brandonwithrow.xcuserdatad → Lottie.xcworkspace/xcuserdata/cal.xcuserdatad}/xcdebugger/Breakpoints_v2.xcbkptlist +2 -2
  26. package/Lottie.xcworkspace/xcuserdata/cal.xcuserdatad/xcdebugger/Expressions.xcexplist +153 -0
  27. package/Lottie.xcworkspace/xcuserdata/calstephens.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  28. package/Lottie.xcworkspace/xcuserdata/calstephens.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +22 -0
  29. package/Lottie.xcworkspace/xcuserdata/calstephens.xcuserdatad/xcdebugger/Expressions.xcexplist +138 -0
  30. package/Lottie.xcworkspace/xcuserdata/calstephens.xcuserdatad/xcschemes/xcschememanagement.plist +30 -0
  31. package/Package.swift +5 -15
  32. package/README.md +40 -60
  33. package/Rakefile +160 -0
  34. package/Sources/Private/CoreAnimation/Animations/CAAnimation+TimingConfiguration.swift +81 -0
  35. package/Sources/Private/CoreAnimation/Animations/CALayer+addAnimation.swift +447 -0
  36. package/Sources/Private/CoreAnimation/Animations/CombinedShapeAnimation.swift +81 -0
  37. package/Sources/Private/CoreAnimation/Animations/CustomPathAnimation.swift +41 -0
  38. package/Sources/Private/CoreAnimation/Animations/EllipseAnimation.swift +55 -0
  39. package/Sources/Private/CoreAnimation/Animations/GradientAnimations.swift +210 -0
  40. package/Sources/Private/CoreAnimation/Animations/LayerProperty.swift +229 -0
  41. package/Sources/Private/CoreAnimation/Animations/OpacityAnimation.swift +52 -0
  42. package/Sources/Private/CoreAnimation/Animations/RectangleAnimation.swift +58 -0
  43. package/Sources/Private/CoreAnimation/Animations/ShapeAnimation.swift +257 -0
  44. package/Sources/Private/CoreAnimation/Animations/StarAnimation.swift +124 -0
  45. package/Sources/Private/CoreAnimation/Animations/StrokeAnimation.swift +73 -0
  46. package/Sources/Private/CoreAnimation/Animations/TransformAnimations.swift +205 -0
  47. package/Sources/Private/CoreAnimation/Animations/VisibilityAnimation.swift +37 -0
  48. package/Sources/Private/CoreAnimation/CompatibilityTracker.swift +130 -0
  49. package/Sources/Private/CoreAnimation/CoreAnimationLayer.swift +497 -0
  50. package/Sources/Private/CoreAnimation/Extensions/CALayer+fillBounds.swift +35 -0
  51. package/Sources/Private/CoreAnimation/Extensions/KeyframeGroup+exactlyOneKeyframe.swift +37 -0
  52. package/Sources/Private/CoreAnimation/Extensions/Keyframes+combinedIfPossible.swift +154 -0
  53. package/Sources/Private/CoreAnimation/Layers/AnimationLayer.swift +83 -0
  54. package/Sources/Private/CoreAnimation/Layers/BaseAnimationLayer.swift +33 -0
  55. package/Sources/Private/CoreAnimation/Layers/BaseCompositionLayer.swift +88 -0
  56. package/Sources/Private/CoreAnimation/Layers/CALayer+setupLayerHierarchy.swift +167 -0
  57. package/Sources/Private/CoreAnimation/Layers/GradientRenderLayer.swift +94 -0
  58. package/Sources/Private/CoreAnimation/Layers/ImageLayer.swift +79 -0
  59. package/Sources/Private/CoreAnimation/Layers/LayerModel+makeAnimationLayer.swift +64 -0
  60. package/Sources/Private/CoreAnimation/Layers/MaskCompositionLayer.swift +138 -0
  61. package/Sources/Private/CoreAnimation/Layers/PreCompLayer.swift +140 -0
  62. package/Sources/Private/CoreAnimation/Layers/RepeaterLayer.swift +85 -0
  63. package/Sources/Private/CoreAnimation/Layers/ShapeItemLayer.swift +315 -0
  64. package/Sources/Private/CoreAnimation/Layers/ShapeLayer.swift +390 -0
  65. package/Sources/Private/CoreAnimation/Layers/SolidLayer.swift +47 -0
  66. package/Sources/Private/CoreAnimation/Layers/TextLayer.swift +91 -0
  67. package/Sources/Private/CoreAnimation/Layers/TransformLayer.swift +11 -0
  68. package/Sources/Private/CoreAnimation/ValueProviderStore.swift +139 -0
  69. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/CompositionLayer.swift +94 -86
  70. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/ImageCompositionLayer.swift +24 -21
  71. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/MaskContainerLayer.swift +75 -54
  72. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/NullCompositionLayer.swift +5 -5
  73. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/PreCompositionLayer.swift +59 -43
  74. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/ShapeCompositionLayer.swift +22 -20
  75. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/SolidCompositionLayer.swift +26 -17
  76. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/CompLayers/TextCompositionLayer.swift +42 -36
  77. package/{lottie-swift/src/Private/LayerContainers/AnimationContainer.swift → Sources/Private/MainThread/LayerContainers/MainThreadAnimationLayer.swift} +200 -140
  78. package/Sources/Private/MainThread/LayerContainers/Utility/CachedImageProvider.swift +47 -0
  79. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/Utility/CompositionLayersInitializer.swift +33 -24
  80. package/{lottie-swift/src/Private/LayerContainers/Utility/TextLayer.swift → Sources/Private/MainThread/LayerContainers/Utility/CoreTextRenderLayer.swift} +149 -106
  81. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/Utility/InvertedMatteLayer.swift +26 -24
  82. package/Sources/Private/MainThread/LayerContainers/Utility/LayerFontProvider.swift +41 -0
  83. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/Utility/LayerImageProvider.swift +20 -16
  84. package/Sources/Private/MainThread/LayerContainers/Utility/LayerTextProvider.swift +40 -0
  85. package/{lottie-swift/src/Private → Sources/Private/MainThread}/LayerContainers/Utility/LayerTransformNode.swift +82 -67
  86. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Extensions/ItemsExtension.swift +9 -4
  87. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/NodeProperty.swift +29 -21
  88. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/Protocols/AnyNodeProperty.swift +16 -10
  89. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/Protocols/AnyValueContainer.swift +6 -6
  90. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/Protocols/KeypathSearchable.swift +5 -5
  91. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/Protocols/NodePropertyMap.swift +10 -8
  92. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/ValueContainer.swift +25 -21
  93. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/ValueProviders/GroupInterpolator.swift +23 -17
  94. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/NodeProperties/ValueProviders/SingleValueProvider.swift +22 -17
  95. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/ModifierNodes/TrimPathNode.swift +110 -79
  96. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/OutputNodes/GroupOutputNode.swift +22 -16
  97. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/OutputNodes/PassThroughOutputNode.swift +19 -15
  98. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/OutputNodes/PathOutputNode.swift +22 -20
  99. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/OutputNodes/Renderables/FillRenderer.swift +22 -23
  100. package/Sources/Private/MainThread/NodeRenderSystem/Nodes/OutputNodes/Renderables/GradientFillRenderer.swift +247 -0
  101. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/OutputNodes/Renderables/GradientStrokeRenderer.swift +30 -24
  102. package/{lottie-swift/src/Private/NodeRenderSystem/Nodes/OutputNodes/Renderables/GradientFillRenderer.swift → Sources/Private/MainThread/NodeRenderSystem/Nodes/OutputNodes/Renderables/LegacyGradientFillRenderer.swift} +93 -66
  103. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/OutputNodes/Renderables/StrokeRenderer.swift +23 -19
  104. package/Sources/Private/MainThread/NodeRenderSystem/Nodes/PathNodes/EllipseNode.swift +139 -0
  105. package/Sources/Private/MainThread/NodeRenderSystem/Nodes/PathNodes/PolygonNode.swift +170 -0
  106. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/PathNodes/RectNode.swift +95 -58
  107. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/PathNodes/ShapeNode.swift +42 -29
  108. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/PathNodes/StarNode.swift +108 -69
  109. package/Sources/Private/MainThread/NodeRenderSystem/Nodes/RenderContainers/GroupNode.swift +155 -0
  110. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/RenderNodes/FillNode.swift +51 -37
  111. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/RenderNodes/GradientFillNode.swift +57 -42
  112. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/RenderNodes/GradientStrokeNode.swift +66 -58
  113. package/Sources/Private/MainThread/NodeRenderSystem/Nodes/RenderNodes/StrokeNode.swift +169 -0
  114. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Nodes/Text/TextAnimatorNode.swift +138 -119
  115. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Protocols/AnimatorNode.swift +80 -79
  116. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Protocols/PathNode.swift +5 -3
  117. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/Protocols/RenderNode.swift +22 -17
  118. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/RenderLayers/ShapeContainerLayer.swift +27 -24
  119. package/{lottie-swift/src/Private → Sources/Private/MainThread}/NodeRenderSystem/RenderLayers/ShapeRenderLayer.swift +34 -25
  120. package/Sources/Private/Model/Animation.swift +160 -0
  121. package/Sources/Private/Model/Assets/Asset.swift +43 -0
  122. package/{lottie-swift/src → Sources}/Private/Model/Assets/AssetLibrary.swift +40 -13
  123. package/Sources/Private/Model/Assets/ImageAsset.swift +112 -0
  124. package/{lottie-swift/src → Sources}/Private/Model/Assets/PrecompAsset.swift +20 -10
  125. package/Sources/Private/Model/DictionaryInitializable.swift +67 -0
  126. package/Sources/Private/Model/Extensions/Bundle.swift +34 -0
  127. package/{lottie-swift/src → Sources}/Private/Model/Extensions/KeyedDecodingContainerExtensions.swift +8 -4
  128. package/Sources/Private/Model/Keyframes/KeyframeData.swift +113 -0
  129. package/Sources/Private/Model/Keyframes/KeyframeGroup.swift +222 -0
  130. package/{lottie-swift/src → Sources}/Private/Model/Layers/ImageLayerModel.swift +21 -11
  131. package/Sources/Private/Model/Layers/LayerModel.swift +228 -0
  132. package/{lottie-swift/src → Sources}/Private/Model/Layers/PreCompLayerModel.swift +39 -22
  133. package/{lottie-swift/src → Sources}/Private/Model/Layers/ShapeLayerModel.swift +23 -12
  134. package/{lottie-swift/src → Sources}/Private/Model/Layers/SolidLayerModel.swift +31 -19
  135. package/{lottie-swift/src → Sources}/Private/Model/Layers/TextLayerModel.swift +33 -19
  136. package/Sources/Private/Model/Objects/DashPattern.swift +44 -0
  137. package/Sources/Private/Model/Objects/Marker.swift +33 -0
  138. package/Sources/Private/Model/Objects/Mask.swift +80 -0
  139. package/Sources/Private/Model/Objects/Transform.swift +179 -0
  140. package/Sources/Private/Model/ShapeItems/Ellipse.swift +74 -0
  141. package/Sources/Private/Model/ShapeItems/Fill.swift +74 -0
  142. package/Sources/Private/Model/ShapeItems/GradientFill.swift +137 -0
  143. package/Sources/Private/Model/ShapeItems/GradientStroke.swift +185 -0
  144. package/Sources/Private/Model/ShapeItems/Group.swift +48 -0
  145. package/{lottie-swift/src → Sources}/Private/Model/ShapeItems/Merge.swift +29 -12
  146. package/Sources/Private/Model/ShapeItems/Rectangle.swift +72 -0
  147. package/Sources/Private/Model/ShapeItems/Repeater.swift +135 -0
  148. package/Sources/Private/Model/ShapeItems/Shape.swift +56 -0
  149. package/Sources/Private/Model/ShapeItems/ShapeItem.swift +163 -0
  150. package/Sources/Private/Model/ShapeItems/ShapeTransform.swift +135 -0
  151. package/Sources/Private/Model/ShapeItems/Star.swift +131 -0
  152. package/Sources/Private/Model/ShapeItems/Stroke.swift +101 -0
  153. package/Sources/Private/Model/ShapeItems/Trim.swift +77 -0
  154. package/Sources/Private/Model/Text/Font.swift +61 -0
  155. package/{lottie-swift/src → Sources}/Private/Model/Text/Glyph.swift +63 -39
  156. package/Sources/Private/Model/Text/TextAnimator.swift +164 -0
  157. package/Sources/Private/Model/Text/TextDocument.swift +123 -0
  158. package/Sources/Private/RootAnimationLayer.swift +51 -0
  159. package/{lottie-swift/src → Sources}/Private/Utility/Debugging/AnimatorNodeDebugging.swift +7 -7
  160. package/{lottie-swift/src → Sources}/Private/Utility/Debugging/LayerDebugging.swift +72 -48
  161. package/Sources/Private/Utility/Debugging/TestHelpers.swift +10 -0
  162. package/{lottie-swift/src → Sources}/Private/Utility/Extensions/AnimationKeypathExtension.swift +69 -59
  163. package/Sources/Private/Utility/Extensions/BlendMode+Filter.swift +31 -0
  164. package/Sources/Private/Utility/Extensions/CGColor+RGB.swift +22 -0
  165. package/{lottie-swift/src → Sources}/Private/Utility/Extensions/CGFloatExtensions.swift +70 -67
  166. package/Sources/Private/Utility/Extensions/DataExtension.swift +27 -0
  167. package/Sources/Private/Utility/Extensions/MathKit.swift +450 -0
  168. package/Sources/Private/Utility/Extensions/StringExtensions.swift +38 -0
  169. package/{lottie-swift/src → Sources}/Private/Utility/Helpers/AnimationContext.swift +42 -16
  170. package/Sources/Private/Utility/Interpolatable/InterpolatableExtensions.swift +134 -0
  171. package/{lottie-swift/src → Sources}/Private/Utility/Interpolatable/KeyframeExtensions.swift +13 -10
  172. package/Sources/Private/Utility/Interpolatable/KeyframeGroup+Extensions.swift +59 -0
  173. package/{lottie-swift/src/Private/NodeRenderSystem/NodeProperties/ValueProviders → Sources/Private/Utility/Interpolatable}/KeyframeInterpolator.swift +125 -108
  174. package/{lottie-swift/src → Sources}/Private/Utility/Primitives/BezierPath.swift +229 -143
  175. package/Sources/Private/Utility/Primitives/CGPointExtension.swift +35 -0
  176. package/{lottie-swift/src → Sources}/Private/Utility/Primitives/ColorExtension.swift +46 -14
  177. package/{lottie-swift/src → Sources}/Private/Utility/Primitives/CompoundBezierPath.swift +58 -49
  178. package/Sources/Private/Utility/Primitives/CurveVertex.swift +184 -0
  179. package/Sources/Private/Utility/Primitives/PathElement.swift +75 -0
  180. package/Sources/Private/Utility/Primitives/UnitBezier.swift +115 -0
  181. package/Sources/Private/Utility/Primitives/VectorsExtensions.swift +341 -0
  182. package/Sources/Public/Animation/AnimationPublic.swift +266 -0
  183. package/Sources/Public/Animation/AnimationView.swift +1335 -0
  184. package/Sources/Public/Animation/AnimationViewInitializers.swift +109 -0
  185. package/Sources/Public/AnimationCache/AnimationCacheProvider.swift +22 -0
  186. package/{lottie-swift/src → Sources}/Public/AnimationCache/LRUAnimationCache.swift +22 -18
  187. package/Sources/Public/DynamicProperties/AnimationKeypath.swift +49 -0
  188. package/Sources/Public/DynamicProperties/AnyValueProvider.swift +132 -0
  189. package/{lottie-swift/src → Sources}/Public/DynamicProperties/ValueProviders/ColorValueProvider.swift +54 -36
  190. package/{lottie-swift/src → Sources}/Public/DynamicProperties/ValueProviders/FloatValueProvider.swift +40 -36
  191. package/Sources/Public/DynamicProperties/ValueProviders/GradientValueProvider.swift +123 -0
  192. package/{lottie-swift/src → Sources}/Public/DynamicProperties/ValueProviders/PointValueProvider.swift +40 -35
  193. package/{lottie-swift/src → Sources}/Public/DynamicProperties/ValueProviders/SizeValueProvider.swift +40 -35
  194. package/{lottie-swift/src → Sources}/Public/FontProvider/AnimationFontProvider.swift +15 -8
  195. package/Sources/Public/ImageProvider/AnimationImageProvider.swift +21 -0
  196. package/Sources/Public/Keyframes/Interpolatable.swift +253 -0
  197. package/Sources/Public/Keyframes/Keyframe.swift +92 -0
  198. package/Sources/Public/Logging/LottieLogger.swift +137 -0
  199. package/Sources/Public/LottieConfiguration.swift +143 -0
  200. package/{lottie-swift/src → Sources}/Public/Primitives/AnimationTime.swift +1 -1
  201. package/{lottie-swift/src → Sources}/Public/Primitives/Color.swift +10 -6
  202. package/{lottie-swift/src → Sources}/Public/Primitives/Vectors.swift +13 -12
  203. package/Sources/Public/TextProvider/AnimationTextProvider.swift +53 -0
  204. package/{lottie-swift/src → Sources}/Public/iOS/AnimatedButton.swift +42 -26
  205. package/{lottie-swift/src → Sources}/Public/iOS/AnimatedControl.swift +100 -91
  206. package/{lottie-swift/src → Sources}/Public/iOS/AnimatedSwitch.swift +128 -94
  207. package/{lottie-swift/src → Sources}/Public/iOS/AnimationSubview.swift +3 -3
  208. package/Sources/Public/iOS/AnimationViewBase.swift +78 -0
  209. package/{lottie-swift/src → Sources}/Public/iOS/BundleImageProvider.swift +36 -34
  210. package/{lottie-swift/src → Sources}/Public/iOS/Compatibility/CompatibleAnimationKeypath.swift +4 -0
  211. package/{lottie-swift/src → Sources}/Public/iOS/Compatibility/CompatibleAnimationView.swift +38 -29
  212. package/{lottie-swift/src → Sources}/Public/iOS/FilepathImageProvider.swift +24 -21
  213. package/{lottie-swift/src → Sources}/Public/iOS/UIColorExtension.swift +4 -4
  214. package/{lottie-swift/src/Public/MacOS/AnimationSubview.swift → Sources/Public/macOS/AnimationSubview.macOS.swift} +4 -5
  215. package/{lottie-swift/src/Public/MacOS/LottieView.swift → Sources/Public/macOS/AnimationViewBase.macOS.swift} +58 -50
  216. package/{lottie-swift/src/Public/MacOS/BundleImageProvider.swift → Sources/Public/macOS/BundleImageProvider.macOS.swift} +31 -32
  217. package/Sources/Public/macOS/FilepathImageProvider.macOS.swift +67 -0
  218. package/Tests/AnimationKeypathTests.swift +103 -0
  219. package/Tests/AutomaticEngineTests.swift +57 -0
  220. package/Tests/BundleTests.swift +25 -0
  221. package/Tests/DataURLTests.swift +64 -0
  222. package/Tests/ParsingTests.swift +43 -0
  223. package/Tests/PerformanceTests.swift +214 -0
  224. package/Tests/Samples/9squares_AlBoardman.json +1 -0
  225. package/Tests/Samples/Boat_Loader.json +1 -0
  226. package/Tests/Samples/HamburgerArrow.json +1 -0
  227. package/Tests/Samples/IconTransitions.json +1 -0
  228. package/Tests/Samples/Images/dog.png +0 -0
  229. package/Tests/Samples/Issues/issue_1125.json +1 -0
  230. package/Tests/Samples/Issues/issue_1260.json +1 -0
  231. package/Tests/Samples/Issues/issue_1403.json +1 -0
  232. package/Tests/Samples/Issues/issue_1407.json +1 -0
  233. package/Tests/Samples/Issues/issue_1460.json +1 -0
  234. package/Tests/Samples/Issues/issue_1488.json +1 -0
  235. package/Tests/Samples/Issues/issue_1505.json +1 -0
  236. package/Tests/Samples/Issues/issue_1541.json +1 -0
  237. package/Tests/Samples/Issues/issue_1557.json +1 -0
  238. package/Tests/Samples/Issues/issue_1603.json +1 -0
  239. package/Tests/Samples/Issues/issue_1628.json +1 -0
  240. package/Tests/Samples/Issues/issue_1636.json +1 -0
  241. package/Tests/Samples/Issues/issue_1643.json +1 -0
  242. package/Tests/Samples/Issues/issue_1655.json +1 -0
  243. package/Tests/Samples/Issues/issue_1664.json +1 -0
  244. package/Tests/Samples/Issues/issue_1683.json +1 -0
  245. package/Tests/Samples/Issues/issue_1687.json +1 -0
  246. package/Tests/Samples/Issues/issue_1711.json +1 -0
  247. package/Tests/Samples/Issues/issue_1717.json +1 -0
  248. package/Tests/Samples/Issues/issue_769.json +1 -0
  249. package/Tests/Samples/Issues/issue_885.json +1 -0
  250. package/Tests/Samples/Issues/issue_965.json +1 -0
  251. package/Tests/Samples/Issues/pr_1536.json +1 -0
  252. package/Tests/Samples/Issues/pr_1563.json +8439 -0
  253. package/Tests/Samples/Issues/pr_1592.json +5527 -0
  254. package/Tests/Samples/Issues/pr_1599.json +738 -0
  255. package/Tests/Samples/Issues/pr_1604_1.json +1 -0
  256. package/Tests/Samples/Issues/pr_1604_2.json +1 -0
  257. package/Tests/Samples/Issues/pr_1632_1.json +1 -0
  258. package/Tests/Samples/Issues/pr_1632_2.json +1 -0
  259. package/Tests/Samples/Issues/pr_1686.json +513 -0
  260. package/Tests/Samples/Issues/pr_1698.json +1 -0
  261. package/Tests/Samples/Issues/pr_1699.json +1 -0
  262. package/Tests/Samples/LottieFiles/LICENSE.md +14 -0
  263. package/Tests/Samples/LottieFiles/bounce_strokes.json +1 -0
  264. package/Tests/Samples/LottieFiles/cactus.json +1 -0
  265. package/Tests/Samples/LottieFiles/dog_car_ride.json +1 -0
  266. package/Tests/Samples/LottieFiles/draft_icon.json +1 -0
  267. package/Tests/Samples/LottieFiles/fireworks.json +1 -0
  268. package/Tests/Samples/LottieFiles/gradient_1.json +1 -0
  269. package/Tests/Samples/LottieFiles/gradient_2.json +1 -0
  270. package/Tests/Samples/LottieFiles/gradient_pill.json +1 -0
  271. package/Tests/Samples/LottieFiles/gradient_shapes.json +1 -0
  272. package/Tests/Samples/LottieFiles/gradient_square.json +1 -0
  273. package/Tests/Samples/LottieFiles/growth.json +1 -0
  274. package/Tests/Samples/LottieFiles/infinity_loader.json +1 -0
  275. package/Tests/Samples/LottieFiles/loading_dots_1.json +1 -0
  276. package/Tests/Samples/LottieFiles/loading_dots_2.json +1 -0
  277. package/Tests/Samples/LottieFiles/loading_dots_3.json +1 -0
  278. package/Tests/Samples/LottieFiles/loading_gradient_strokes.json +1 -0
  279. package/Tests/Samples/LottieFiles/settings_slider.json +1 -0
  280. package/Tests/Samples/LottieFiles/shop.json +1 -0
  281. package/Tests/Samples/LottieFiles/step_loader.json +1 -0
  282. package/Tests/Samples/LottieLogo1.json +1 -0
  283. package/Tests/Samples/LottieLogo1_masked.json +1 -0
  284. package/Tests/Samples/LottieLogo2.json +1 -0
  285. package/Tests/Samples/MotionCorpse_Jrcanest.json +1 -0
  286. package/Tests/Samples/Nonanimating/BasicLayers.json +1 -0
  287. package/Tests/Samples/Nonanimating/DisableNodesTest.json +1 -0
  288. package/Tests/Samples/Nonanimating/FirstText.json +1 -0
  289. package/Tests/Samples/Nonanimating/GeometryTransformTest.json +1 -0
  290. package/Tests/Samples/Nonanimating/Text_AnimatedProperties.json +1 -0
  291. package/Tests/Samples/Nonanimating/Text_Glyph.json +1 -0
  292. package/Tests/Samples/Nonanimating/Text_NoAnimation.json +1 -0
  293. package/Tests/Samples/Nonanimating/Text_NoGlyph.json +1 -0
  294. package/Tests/Samples/Nonanimating/Zoom.json +1 -0
  295. package/Tests/Samples/Nonanimating/_dog.json +1 -0
  296. package/Tests/Samples/Nonanimating/base64Test.json +1 -0
  297. package/Tests/Samples/Nonanimating/blend_mode_test.json +1 -0
  298. package/Tests/Samples/Nonanimating/keypathTest.json +1 -0
  299. package/Tests/Samples/Nonanimating/verifyLineHeight.json +1 -0
  300. package/Tests/Samples/PinJump.json +1 -0
  301. package/Tests/Samples/Switch.json +1 -0
  302. package/Tests/Samples/Switch_States.json +1 -0
  303. package/Tests/Samples/TwitterHeart.json +1 -0
  304. package/Tests/Samples/TwitterHeartButton.json +1 -0
  305. package/Tests/Samples/TypeFace/A.json +1 -0
  306. package/Tests/Samples/TypeFace/Apostrophe.json +1 -0
  307. package/Tests/Samples/TypeFace/B.json +1 -0
  308. package/Tests/Samples/TypeFace/BlinkingCursor.json +1 -0
  309. package/Tests/Samples/TypeFace/C.json +1 -0
  310. package/Tests/Samples/TypeFace/Colon.json +1 -0
  311. package/Tests/Samples/TypeFace/Comma.json +1 -0
  312. package/Tests/Samples/TypeFace/D.json +1 -0
  313. package/Tests/Samples/TypeFace/E.json +1 -0
  314. package/Tests/Samples/TypeFace/F.json +1 -0
  315. package/Tests/Samples/TypeFace/G.json +1 -0
  316. package/Tests/Samples/TypeFace/H.json +1 -0
  317. package/Tests/Samples/TypeFace/I.json +1 -0
  318. package/Tests/Samples/TypeFace/J.json +1 -0
  319. package/Tests/Samples/TypeFace/K.json +1 -0
  320. package/Tests/Samples/TypeFace/L.json +1 -0
  321. package/Tests/Samples/TypeFace/M.json +1 -0
  322. package/Tests/Samples/TypeFace/N.json +1 -0
  323. package/Tests/Samples/TypeFace/O.json +1 -0
  324. package/Tests/Samples/TypeFace/P.json +1 -0
  325. package/Tests/Samples/TypeFace/Q.json +1 -0
  326. package/Tests/Samples/TypeFace/R.json +1 -0
  327. package/Tests/Samples/TypeFace/S.json +1 -0
  328. package/Tests/Samples/TypeFace/T.json +1 -0
  329. package/Tests/Samples/TypeFace/U.json +1 -0
  330. package/Tests/Samples/TypeFace/V.json +1 -0
  331. package/Tests/Samples/TypeFace/W.json +1 -0
  332. package/Tests/Samples/TypeFace/X.json +1 -0
  333. package/Tests/Samples/TypeFace/Y.json +1 -0
  334. package/Tests/Samples/TypeFace/Z.json +1 -0
  335. package/Tests/Samples/Watermelon.json +1 -0
  336. package/Tests/Samples/setValueTest.json +1 -0
  337. package/Tests/Samples/timeremap.json +1 -0
  338. package/Tests/Samples/vcTransition1.json +1 -0
  339. package/Tests/Samples/vcTransition2.json +1 -0
  340. package/Tests/SnapshotConfiguration.swift +158 -0
  341. package/Tests/SnapshotTests.swift +265 -0
  342. package/Tests/Utils/Bundle+Module.swift +30 -0
  343. package/Tests/Utils/HardcodedFontProvider.swift +19 -0
  344. package/Tests/Utils/HardcodedImageProvider.swift +23 -0
  345. package/Tests/Utils/Snapshotting+presentationLayer.swift +47 -0
  346. package/Tests/ValueProvidersTests.swift +27 -0
  347. package/lottie-ios.podspec +12 -12
  348. package/package.json +1 -1
  349. package/script/test-carthage/Cartfile +1 -0
  350. package/script/test-carthage/Cartfile.resolved +1 -0
  351. package/script/test-carthage/CarthageTest/AppDelegate.swift +26 -0
  352. package/script/test-carthage/CarthageTest/Assets.xcassets/AccentColor.colorset/Contents.json +11 -0
  353. package/script/test-carthage/CarthageTest/Assets.xcassets/AppIcon.appiconset/Contents.json +98 -0
  354. package/script/test-carthage/CarthageTest/Assets.xcassets/Contents.json +6 -0
  355. package/script/test-carthage/CarthageTest/Base.lproj/LaunchScreen.storyboard +25 -0
  356. package/script/test-carthage/CarthageTest/Base.lproj/Main.storyboard +24 -0
  357. package/script/test-carthage/CarthageTest/Info.plist +66 -0
  358. package/script/test-carthage/CarthageTest/SceneDelegate.swift +10 -0
  359. package/script/test-carthage/CarthageTest/ViewController.swift +15 -0
  360. package/script/test-carthage/CarthageTest-macOS/AppDelegate.swift +7 -0
  361. package/script/test-carthage/CarthageTest-macOS/Assets.xcassets/AccentColor.colorset/Contents.json +11 -0
  362. package/script/test-carthage/CarthageTest-macOS/Assets.xcassets/AppIcon.appiconset/Contents.json +58 -0
  363. package/script/test-carthage/CarthageTest-macOS/Assets.xcassets/Contents.json +6 -0
  364. package/script/test-carthage/CarthageTest-macOS/Base.lproj/Main.storyboard +717 -0
  365. package/script/test-carthage/CarthageTest-macOS/CarthageTest_macOS.entitlements +10 -0
  366. package/script/test-carthage/CarthageTest-macOS/ViewController.swift +15 -0
  367. package/script/test-carthage/CarthageTest.xcodeproj/project.pbxproj +532 -0
  368. package/script/test-carthage/CarthageTest.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  369. package/script/test-carthage/CarthageTest.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  370. package/script/test-carthage/CarthageTest.xcodeproj/project.xcworkspace/xcuserdata/cal.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  371. package/script/test-carthage/CarthageTest.xcodeproj/project.xcworkspace/xcuserdata/calstephens.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  372. package/script/test-carthage/CarthageTest.xcodeproj/xcuserdata/cal.xcuserdatad/xcschemes/xcschememanagement.plist +14 -0
  373. package/script/test-carthage/CarthageTest.xcodeproj/xcuserdata/calstephens.xcuserdatad/xcschemes/xcschememanagement.plist +19 -0
  374. package/script/test-carthage/Mintfile +1 -0
  375. package/script/test-spm/LottieSPM.xcworkspace/contents.xcworkspacedata +7 -0
  376. package/script/test-spm/LottieSPM.xcworkspace/xcuserdata/cal.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  377. package/script/test-spm/Mintfile +1 -0
  378. package/.swiftpm/xcode/package.xcworkspace/xcuserdata/brandonwithrow.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  379. package/Lottie/Info.plist +0 -22
  380. package/Lottie.xcodeproj/project.xcworkspace/xcuserdata/brandonwithrow.xcuserdatad/UserInterfaceState.xcuserstate +0 -0
  381. package/Lottie.xcodeproj/xcuserdata/brandonwithrow.xcuserdatad/xcschemes/LottieLibraryMacOS.xcscheme +0 -80
  382. package/Lottie.xcodeproj/xcuserdata/brandonwithrow.xcuserdatad/xcschemes/xcschememanagement.plist +0 -57
  383. package/lottie-swift/Assets/.gitkeep +0 -0
  384. package/lottie-swift/src/Private/LayerContainers/Utility/LayerFontProvider.swift +0 -37
  385. package/lottie-swift/src/Private/LayerContainers/Utility/LayerTextProvider.swift +0 -36
  386. package/lottie-swift/src/Private/Model/Animation.swift +0 -107
  387. package/lottie-swift/src/Private/Model/Assets/Asset.swift +0 -27
  388. package/lottie-swift/src/Private/Model/Assets/ImageAsset.swift +0 -48
  389. package/lottie-swift/src/Private/Model/Keyframes/Keyframe.swift +0 -128
  390. package/lottie-swift/src/Private/Model/Keyframes/KeyframeGroup.swift +0 -108
  391. package/lottie-swift/src/Private/Model/Layers/LayerModel.swift +0 -150
  392. package/lottie-swift/src/Private/Model/Objects/DashPattern.swift +0 -24
  393. package/lottie-swift/src/Private/Model/Objects/Marker.swift +0 -23
  394. package/lottie-swift/src/Private/Model/Objects/Mask.swift +0 -48
  395. package/lottie-swift/src/Private/Model/Objects/Transform.swift +0 -105
  396. package/lottie-swift/src/Private/Model/ShapeItems/Ellipse.swift +0 -50
  397. package/lottie-swift/src/Private/Model/ShapeItems/FillI.swift +0 -49
  398. package/lottie-swift/src/Private/Model/ShapeItems/GradientFill.swift +0 -86
  399. package/lottie-swift/src/Private/Model/ShapeItems/GradientStroke.swift +0 -125
  400. package/lottie-swift/src/Private/Model/ShapeItems/Group.swift +0 -32
  401. package/lottie-swift/src/Private/Model/ShapeItems/Rectangle.swift +0 -50
  402. package/lottie-swift/src/Private/Model/ShapeItems/Repeater.swift +0 -80
  403. package/lottie-swift/src/Private/Model/ShapeItems/Shape.swift +0 -37
  404. package/lottie-swift/src/Private/Model/ShapeItems/ShapeItem.swift +0 -95
  405. package/lottie-swift/src/Private/Model/ShapeItems/ShapeTransform.swift +0 -68
  406. package/lottie-swift/src/Private/Model/ShapeItems/Star.swift +0 -86
  407. package/lottie-swift/src/Private/Model/ShapeItems/Stroke.swift +0 -67
  408. package/lottie-swift/src/Private/Model/ShapeItems/Trim.swift +0 -53
  409. package/lottie-swift/src/Private/Model/Text/Font.swift +0 -35
  410. package/lottie-swift/src/Private/Model/Text/TextAnimator.swift +0 -99
  411. package/lottie-swift/src/Private/Model/Text/TextDocument.swift +0 -70
  412. package/lottie-swift/src/Private/NodeRenderSystem/Nodes/PathNodes/EllipseNode.swift +0 -109
  413. package/lottie-swift/src/Private/NodeRenderSystem/Nodes/PathNodes/PolygonNode.swift +0 -132
  414. package/lottie-swift/src/Private/NodeRenderSystem/Nodes/RenderContainers/GroupNode.swift +0 -141
  415. package/lottie-swift/src/Private/NodeRenderSystem/Nodes/RenderNodes/StrokeNode.swift +0 -127
  416. package/lottie-swift/src/Private/Utility/Extensions/MathKit.swift +0 -539
  417. package/lottie-swift/src/Private/Utility/Extensions/StringExtensions.swift +0 -32
  418. package/lottie-swift/src/Private/Utility/Interpolatable/Interpolatable.swift +0 -18
  419. package/lottie-swift/src/Private/Utility/Interpolatable/InterpolatableExtensions.swift +0 -170
  420. package/lottie-swift/src/Private/Utility/Primitives/CurveVertex.swift +0 -177
  421. package/lottie-swift/src/Private/Utility/Primitives/PathElement.swift +0 -68
  422. package/lottie-swift/src/Private/Utility/Primitives/VectorsExtensions.swift +0 -218
  423. package/lottie-swift/src/Public/Animation/AnimationPublic.swift +0 -196
  424. package/lottie-swift/src/Public/Animation/AnimationView.swift +0 -1006
  425. package/lottie-swift/src/Public/Animation/AnimationViewInitializers.swift +0 -83
  426. package/lottie-swift/src/Public/AnimationCache/AnimationCacheProvider.swift +0 -24
  427. package/lottie-swift/src/Public/DynamicProperties/AnimationKeypath.swift +0 -46
  428. package/lottie-swift/src/Public/DynamicProperties/AnyValueProvider.swift +0 -29
  429. package/lottie-swift/src/Public/DynamicProperties/ValueProviders/GradientValueProvider.swift +0 -114
  430. package/lottie-swift/src/Public/ImageProvider/AnimationImageProvider.swift +0 -23
  431. package/lottie-swift/src/Public/MacOS/FilepathImageProvider.swift +0 -67
  432. package/lottie-swift/src/Public/TextProvider/AnimationTextProvider.swift +0 -39
  433. package/lottie-swift/src/Public/iOS/LottieView.swift +0 -62
  434. package/lottie-swift-testing.podspec +0 -32
@@ -1,1006 +0,0 @@
1
- //
2
- // LottieView.swift
3
- // lottie-swift
4
- //
5
- // Created by Brandon Withrow on 1/23/19.
6
- //
7
-
8
- import Foundation
9
- import QuartzCore
10
-
11
- /// Describes the behavior of an AnimationView when the app is moved to the background.
12
- public enum LottieBackgroundBehavior {
13
- /// Stop the animation and reset it to the beginning of its current play time. The completion block is called.
14
- case stop
15
- /// Pause the animation in its current state. The completion block is called.
16
- case pause
17
- /// Pause the animation and restart it when the application moves to the foreground. The completion block is stored and called when the animation completes.
18
- case pauseAndRestore
19
- /// Stops the animation and sets it to the end of its current play time. The completion block is called.
20
- case forceFinish
21
- }
22
-
23
- /// Defines animation loop behavior
24
- public enum LottieLoopMode {
25
- /// Animation is played once then stops.
26
- case playOnce
27
- /// Animation will loop from beginning to end until stopped.
28
- case loop
29
- /// Animation will play forward, then backwards and loop until stopped.
30
- case autoReverse
31
- /// Animation will loop from beginning to end up to defined amount of times.
32
- case `repeat`(Float)
33
- /// Animation will play forward, then backwards a defined amount of times.
34
- case repeatBackwards(Float)
35
- }
36
-
37
- extension LottieLoopMode: Equatable {
38
- public static func == (lhs: LottieLoopMode, rhs: LottieLoopMode) -> Bool {
39
- switch (lhs, rhs) {
40
- case (.repeat(let lhsAmount), .repeat(let rhsAmount)),
41
- (.repeatBackwards(let lhsAmount), .repeatBackwards(let rhsAmount)):
42
- return lhsAmount == rhsAmount
43
- case (.playOnce, .playOnce),
44
- (.loop, .loop),
45
- (.autoReverse, .autoReverse):
46
- return true
47
- default:
48
- return false
49
- }
50
- }
51
- }
52
-
53
- @IBDesignable
54
- final public class AnimationView: LottieView {
55
-
56
- // MARK: - Public Properties
57
-
58
- /**
59
- Sets the animation backing the animation view. Setting this will clear the
60
- view's contents, completion blocks and current state. The new animation will
61
- be loaded up and set to the beginning of its timeline.
62
- */
63
- public var animation: Animation? {
64
- didSet {
65
- makeAnimationLayer()
66
- }
67
- }
68
-
69
- /// Set animation name from Interface Builder
70
- @IBInspectable var animationName: String? {
71
- didSet {
72
- self.animation = animationName.flatMap {
73
- Animation.named($0, animationCache: nil)
74
- }
75
- }
76
- }
77
-
78
- /**
79
- Describes the behavior of an AnimationView when the app is moved to the background.
80
-
81
- The default is `pause` which pauses the animation when the application moves to
82
- the background. The completion block is called with `false` for completed.
83
- */
84
- public var backgroundBehavior: LottieBackgroundBehavior = .pause
85
-
86
- /**
87
- Sets the image provider for the animation view. An image provider provides the
88
- animation with its required image data.
89
-
90
- Setting this will cause the animation to reload its image contents.
91
- */
92
- public var imageProvider: AnimationImageProvider {
93
- didSet {
94
- animationLayer?.imageProvider = imageProvider
95
- reloadImages()
96
- }
97
- }
98
-
99
- /**
100
- Sets the text provider for animation view. A text provider provides the
101
- animation with values for text layers
102
- */
103
- public var textProvider: AnimationTextProvider {
104
- didSet {
105
- animationLayer?.textProvider = textProvider
106
- }
107
- }
108
-
109
- /**
110
- Sets the text provider for animation view. A text provider provides the
111
- animation with values for text layers
112
- */
113
- public var fontProvider: AnimationFontProvider {
114
- didSet {
115
- animationLayer?.fontProvider = fontProvider
116
- }
117
- }
118
-
119
- /// Returns `true` if the animation is currently playing.
120
- public var isAnimationPlaying: Bool {
121
- return animationLayer?.animation(forKey: activeAnimationName) != nil
122
- }
123
-
124
- /// Returns `true` if the animation will start playing when this view is added to a window.
125
- public var isAnimationQueued: Bool {
126
- return animationContext != nil && waitingToPlayAnimation
127
- }
128
-
129
- /// Sets the loop behavior for `play` calls. Defaults to `playOnce`
130
- public var loopMode: LottieLoopMode = .playOnce {
131
- didSet {
132
- updateInFlightAnimation()
133
- }
134
- }
135
-
136
- /**
137
- When `true` the animation view will rasterize its contents when not animating.
138
- Rasterizing will improve performance of static animations.
139
-
140
- Note: this will not produce crisp results at resolutions above the animations natural resolution.
141
-
142
- Defaults to `false`
143
- */
144
- public var shouldRasterizeWhenIdle: Bool = false {
145
- didSet {
146
- updateRasterizationState()
147
- }
148
- }
149
-
150
- /**
151
- Sets the current animation time with a Progress Time
152
-
153
- Note: Setting this will stop the current animation, if any.
154
- Note 2: If `animation` is nil, setting this will fallback to 0
155
- */
156
- public var currentProgress: AnimationProgressTime {
157
- set {
158
- if let animation = animation {
159
- currentFrame = animation.frameTime(forProgress: newValue)
160
- } else {
161
- currentFrame = 0
162
- }
163
- }
164
- get {
165
- if let animation = animation {
166
- return animation.progressTime(forFrame: currentFrame)
167
- } else {
168
- return 0
169
- }
170
- }
171
- }
172
-
173
- /**
174
- Sets the current animation time with a time in seconds.
175
-
176
- Note: Setting this will stop the current animation, if any.
177
- Note 2: If `animation` is nil, setting this will fallback to 0
178
- */
179
- public var currentTime: TimeInterval {
180
- set {
181
- if let animation = animation {
182
- currentFrame = animation.frameTime(forTime: newValue)
183
- } else {
184
- currentFrame = 0
185
- }
186
- }
187
- get {
188
- if let animation = animation {
189
- return animation.time(forFrame: currentFrame)
190
- } else {
191
- return 0
192
- }
193
- }
194
- }
195
-
196
- /**
197
- Sets the current animation time with a frame in the animations framerate.
198
-
199
- Note: Setting this will stop the current animation, if any.
200
- */
201
- public var currentFrame: AnimationFrameTime {
202
- set {
203
- removeCurrentAnimation()
204
- updateAnimationFrame(newValue)
205
- }
206
- get {
207
- return animationLayer?.currentFrame ?? 0
208
- }
209
- }
210
-
211
- /// Returns the current animation frame while an animation is playing.
212
- public var realtimeAnimationFrame: AnimationFrameTime {
213
- return isAnimationPlaying ? animationLayer?.presentation()?.currentFrame ?? currentFrame : currentFrame
214
- }
215
-
216
- /// Returns the current animation frame while an animation is playing.
217
- public var realtimeAnimationProgress: AnimationProgressTime {
218
- if let animation = animation {
219
- return animation.progressTime(forFrame: realtimeAnimationFrame)
220
- }
221
- return 0
222
- }
223
-
224
- /// Sets the speed of the animation playback. Defaults to 1
225
- public var animationSpeed: CGFloat = 1 {
226
- didSet {
227
- updateInFlightAnimation()
228
- }
229
- }
230
-
231
- /**
232
- When `true` the animation will play back at the framerate encoded in the
233
- `Animation` model. When `false` the animation will play at the framerate
234
- of the device.
235
-
236
- Defaults to false
237
- */
238
- public var respectAnimationFrameRate: Bool = false {
239
- didSet {
240
- animationLayer?.respectAnimationFrameRate = respectAnimationFrameRate
241
- }
242
- }
243
-
244
- /**
245
- Controls the cropping of an Animation. Setting this property will crop the animation
246
- to the current views bounds by the viewport frame. The coordinate space is specified
247
- in the animation's coordinate space.
248
-
249
- Animatable.
250
- */
251
- public var viewportFrame: CGRect? = nil {
252
- didSet {
253
-
254
- /*
255
- This is really ugly, but is needed to trigger a layout pass within an animation block.
256
- Typically this happens automatically, when layout objects are UIView based.
257
- The animation layer is a CALayer which will not implicitly grab the animation
258
- duration of a UIView animation block.
259
-
260
- By setting bounds and then resetting bounds the UIView animation block's
261
- duration and curve are captured and added to the layer. This is used in the
262
- layout block to animate the animationLayer's position and size.
263
- */
264
- let rect = bounds
265
- self.bounds = CGRect.zero
266
- self.bounds = rect
267
- self.setNeedsLayout()
268
- }
269
- }
270
-
271
- // MARK: - Public Functions
272
-
273
- /**
274
- Plays the animation from its current state to the end.
275
-
276
- - Parameter completion: An optional completion closure to be called when the animation completes playing.
277
- */
278
- public func play(completion: LottieCompletionBlock? = nil) {
279
- guard let animation = animation else {
280
- return
281
- }
282
-
283
- /// Build a context for the animation.
284
- let context = AnimationContext(playFrom: CGFloat(animation.startFrame),
285
- playTo: CGFloat(animation.endFrame),
286
- closure: completion)
287
- removeCurrentAnimation()
288
- addNewAnimationForContext(context)
289
- }
290
-
291
- /**
292
- Plays the animation from a progress (0-1) to a progress (0-1).
293
-
294
- - Parameter fromProgress: The start progress of the animation. If `nil` the animation will start at the current progress.
295
- - Parameter toProgress: The end progress of the animation.
296
- - Parameter loopMode: The loop behavior of the animation. If `nil` the view's `loopMode` property will be used.
297
- - Parameter completion: An optional completion closure to be called when the animation stops.
298
- */
299
- public func play(fromProgress: AnimationProgressTime? = nil,
300
- toProgress: AnimationProgressTime,
301
- loopMode: LottieLoopMode? = nil,
302
- completion: LottieCompletionBlock? = nil) {
303
- guard let animation = animation else {
304
- return
305
- }
306
-
307
- removeCurrentAnimation()
308
- if let loopMode = loopMode {
309
- /// Set the loop mode, if one was supplied
310
- self.loopMode = loopMode
311
- }
312
- let context = AnimationContext(playFrom: animation.frameTime(forProgress: fromProgress ?? currentProgress),
313
- playTo: animation.frameTime(forProgress: toProgress),
314
- closure: completion)
315
- addNewAnimationForContext(context)
316
- }
317
-
318
- /**
319
- Plays the animation from a start frame to an end frame in the animation's framerate.
320
-
321
- - Parameter fromFrame: The start frame of the animation. If `nil` the animation will start at the current frame.
322
- - Parameter toFrame: The end frame of the animation.
323
- - Parameter loopMode: The loop behavior of the animation. If `nil` the view's `loopMode` property will be used.
324
- - Parameter completion: An optional completion closure to be called when the animation stops.
325
- */
326
- public func play(fromFrame: AnimationFrameTime? = nil,
327
- toFrame: AnimationFrameTime,
328
- loopMode: LottieLoopMode? = nil,
329
- completion: LottieCompletionBlock? = nil) {
330
- removeCurrentAnimation()
331
- if let loopMode = loopMode {
332
- /// Set the loop mode, if one was supplied
333
- self.loopMode = loopMode
334
- }
335
-
336
- let context = AnimationContext(playFrom: fromFrame ?? currentProgress,
337
- playTo: toFrame,
338
- closure: completion)
339
- addNewAnimationForContext(context)
340
- }
341
-
342
- /**
343
- Plays the animation from a named marker to another marker.
344
-
345
- Markers are point in time that are encoded into the Animation data and assigned
346
- a name.
347
-
348
- NOTE: If markers are not found the play command will exit.
349
-
350
- - Parameter fromMarker: The start marker for the animation playback. If `nil` the
351
- animation will start at the current progress.
352
- - Parameter toMarker: The end marker for the animation playback.
353
- - Parameter loopMode: The loop behavior of the animation. If `nil` the view's `loopMode` property will be used.
354
- - Parameter completion: An optional completion closure to be called when the animation stops.
355
- */
356
- public func play(fromMarker: String? = nil,
357
- toMarker: String,
358
- loopMode: LottieLoopMode? = nil,
359
- completion: LottieCompletionBlock? = nil) {
360
-
361
- guard let animation = animation, let markers = animation.markerMap, let to = markers[toMarker] else {
362
- return
363
- }
364
-
365
- removeCurrentAnimation()
366
- if let loopMode = loopMode {
367
- /// Set the loop mode, if one was supplied
368
- self.loopMode = loopMode
369
- }
370
-
371
- let fromTime: CGFloat
372
- if let fromName = fromMarker, let from = markers[fromName] {
373
- fromTime = CGFloat(from.frameTime)
374
- } else {
375
- fromTime = currentFrame
376
- }
377
-
378
- let context = AnimationContext(playFrom: fromTime,
379
- playTo: CGFloat(to.frameTime),
380
- closure: completion)
381
- addNewAnimationForContext(context)
382
- }
383
-
384
- /**
385
- Stops the animation and resets the view to its start frame.
386
-
387
- The completion closure will be called with `false`
388
- */
389
- public func stop() {
390
- removeCurrentAnimation()
391
- currentFrame = 0
392
- }
393
-
394
- /**
395
- Pauses the animation in its current state.
396
-
397
- The completion closure will be called with `false`
398
- */
399
- public func pause() {
400
- removeCurrentAnimation()
401
- }
402
-
403
- /// Reloads the images supplied to the animation from the `imageProvider`
404
- public func reloadImages() {
405
- animationLayer?.reloadImages()
406
- }
407
-
408
- /// Forces the AnimationView to redraw its contents.
409
- public func forceDisplayUpdate() {
410
- animationLayer?.forceDisplayUpdate()
411
- }
412
-
413
- // MARK: - Public (Dynamic Properties)
414
-
415
- /**
416
-
417
- Sets a ValueProvider for the specified keypath. The value provider will be set
418
- on all properties that match the keypath.
419
-
420
- Nearly all properties of a Lottie animation can be changed at runtime using a
421
- combination of `Animation Keypaths` and `Value Providers`.
422
- Setting a ValueProvider on a keypath will cause the animation to update its
423
- contents and read the new Value Provider.
424
-
425
- A value provider provides a typed value on a frame by frame basis.
426
-
427
- - Parameter valueProvider: The new value provider for the properties.
428
- - Parameter keypath: The keypath used to search for properties.
429
-
430
- Example:
431
- ```
432
- /// A keypath that finds the color value for all `Fill 1` nodes.
433
- let fillKeypath = AnimationKeypath(keypath: "**.Fill 1.Color")
434
- /// A Color Value provider that returns a reddish color.
435
- let redValueProvider = ColorValueProvider(Color(r: 1, g: 0.2, b: 0.3, a: 1))
436
- /// Set the provider on the animationView.
437
- animationView.setValueProvider(redValueProvider, keypath: fillKeypath)
438
- ```
439
- */
440
- public func setValueProvider(_ valueProvider: AnyValueProvider, keypath: AnimationKeypath) {
441
- animationLayer?.setValueProvider(valueProvider, keypath: keypath)
442
- }
443
- /**
444
- Reads the value of a property specified by the Keypath.
445
- Returns nil if no property is found.
446
-
447
- - Parameter for: The keypath used to search for the property.
448
- - Parameter atFrame: The Frame Time of the value to query. If nil then the current frame is used.
449
- */
450
- public func getValue(for keypath: AnimationKeypath, atFrame: AnimationFrameTime?) -> Any? {
451
- return animationLayer?.getValue(for: keypath, atFrame: atFrame)
452
- }
453
-
454
- /// Logs all child keypaths.
455
- public func logHierarchyKeypaths() {
456
- animationLayer?.logHierarchyKeypaths()
457
- }
458
-
459
- // MARK: - Public (Add Subview)
460
-
461
- /**
462
- Searches for the nearest child layer to the first Keypath and adds the subview
463
- to that layer. The subview will move and animate with the child layer.
464
- Furthermore the subview will be in the child layers coordinate space.
465
-
466
- Note: if no layer is found for the keypath, then nothing happens.
467
-
468
- - Parameter subview: The subview to add to the found animation layer.
469
- - Parameter keypath: The keypath used to find the animation layer.
470
-
471
- Example:
472
- ```
473
- /// A keypath that finds `Layer 1`
474
- let layerKeypath = AnimationKeypath(keypath: "Layer 1")
475
-
476
- /// Wrap the custom view in an `AnimationSubview`
477
- let subview = AnimationSubview()
478
- subview.addSubview(customView)
479
-
480
- /// Set the provider on the animationView.
481
- animationView.addSubview(subview, forLayerAt: layerKeypath)
482
- ```
483
- */
484
- public func addSubview(_ subview: AnimationSubview, forLayerAt keypath: AnimationKeypath) {
485
- guard let sublayer = animationLayer?.layer(for: keypath) else {
486
- return
487
- }
488
- self.setNeedsLayout()
489
- self.layoutIfNeeded()
490
- self.forceDisplayUpdate()
491
- addSubview(subview)
492
- if let subViewLayer = subview.viewLayer {
493
- sublayer.addSublayer(subViewLayer)
494
- }
495
- }
496
-
497
- /**
498
- Converts a CGRect from the AnimationView's coordinate space into the
499
- coordinate space of the layer found at Keypath.
500
-
501
- If no layer is found, nil is returned
502
-
503
- - Parameter rect: The CGRect to convert.
504
- - Parameter toLayerAt: The keypath used to find the layer.
505
- */
506
- public func convert(_ rect: CGRect, toLayerAt keypath: AnimationKeypath?) -> CGRect? {
507
- guard let animationLayer = animationLayer else { return nil }
508
- guard let keypath = keypath else {
509
- return viewLayer?.convert(rect, to: animationLayer)
510
- }
511
- guard let sublayer = animationLayer.layer(for: keypath) else {
512
- return nil
513
- }
514
- self.setNeedsLayout()
515
- self.layoutIfNeeded()
516
- self.forceDisplayUpdate()
517
- return animationLayer.convert(rect, to: sublayer)
518
- }
519
-
520
- /**
521
- Converts a CGPoint from the AnimationView's coordinate space into the
522
- coordinate space of the layer found at Keypath.
523
-
524
- If no layer is found, nil is returned
525
-
526
- - Parameter point: The CGPoint to convert.
527
- - Parameter toLayerAt: The keypath used to find the layer.
528
- */
529
- public func convert(_ point: CGPoint, toLayerAt keypath: AnimationKeypath?) -> CGPoint? {
530
- guard let animationLayer = animationLayer else { return nil }
531
- guard let keypath = keypath else {
532
- return viewLayer?.convert(point, to: animationLayer)
533
- }
534
- guard let sublayer = animationLayer.layer(for: keypath) else {
535
- return nil
536
- }
537
- self.setNeedsLayout()
538
- self.layoutIfNeeded()
539
- self.forceDisplayUpdate()
540
- return animationLayer.convert(point, to: sublayer)
541
- }
542
-
543
- // MARK: - Public (Animation Contents)
544
-
545
- /**
546
- Sets the enabled state of all animator nodes found with the keypath search.
547
- This can be used to interactively enable / disable parts of the animation.
548
-
549
- - Parameter isEnabled: When true the animator nodes affect the rendering tree. When false the node is removed from the tree.
550
- - Parameter keypath: The keypath used to find the node(s).
551
- */
552
- public func setNodeIsEnabled(isEnabled: Bool, keypath: AnimationKeypath) {
553
- guard let animationLayer = animationLayer else { return }
554
- let nodes = animationLayer.animatorNodes(for: keypath)
555
- if let nodes = nodes {
556
- for node in nodes {
557
- node.isEnabled = isEnabled
558
- }
559
- self.forceDisplayUpdate()
560
- }
561
- }
562
-
563
- // MARK: - Public (Markers)
564
-
565
- /**
566
- Markers are a way to describe a point in time by a key name.
567
-
568
- Markers are encoded into animation JSON. By using markers a designer can mark
569
- playback points for a developer to use without having to worry about keeping
570
- track of animation frames. If the animation file is updated, the developer
571
- does not need to update playback code.
572
-
573
- Returns the Progress Time for the marker named. Returns nil if no marker found.
574
- */
575
- public func progressTime(forMarker named: String) -> AnimationProgressTime? {
576
- guard let animation = animation else {
577
- return nil
578
- }
579
- return animation.progressTime(forMarker: named)
580
- }
581
-
582
- /**
583
- Markers are a way to describe a point in time by a key name.
584
-
585
- Markers are encoded into animation JSON. By using markers a designer can mark
586
- playback points for a developer to use without having to worry about keeping
587
- track of animation frames. If the animation file is updated, the developer
588
- does not need to update playback code.
589
-
590
- Returns the Frame Time for the marker named. Returns nil if no marker found.
591
- */
592
- public func frameTime(forMarker named: String) -> AnimationFrameTime? {
593
- guard let animation = animation else {
594
- return nil
595
- }
596
- return animation.frameTime(forMarker: named)
597
- }
598
-
599
- // MARK: - Public (Initializers)
600
-
601
- /// Initializes a LottieView with an animation.
602
- public init(
603
- animation: Animation?,
604
- imageProvider: AnimationImageProvider? = nil,
605
- textProvider: AnimationTextProvider = DefaultTextProvider(),
606
- fontProvider: AnimationFontProvider = DefaultFontProvider()) {
607
- self.animation = animation
608
- self.imageProvider = imageProvider ?? BundleImageProvider(bundle: Bundle.main, searchPath: nil)
609
- self.textProvider = textProvider
610
- self.fontProvider = fontProvider
611
- super.init(frame: .zero)
612
- commonInit()
613
- makeAnimationLayer()
614
- if let animation = animation {
615
- frame = animation.bounds
616
- }
617
- }
618
-
619
- public init() {
620
- self.animation = nil
621
- self.imageProvider = BundleImageProvider(bundle: Bundle.main, searchPath: nil)
622
- self.textProvider = DefaultTextProvider()
623
- self.fontProvider = DefaultFontProvider()
624
- super.init(frame: .zero)
625
- commonInit()
626
- }
627
-
628
- public override init(frame: CGRect) {
629
- self.animation = nil
630
- self.imageProvider = BundleImageProvider(bundle: Bundle.main, searchPath: nil)
631
- self.textProvider = DefaultTextProvider()
632
- self.fontProvider = DefaultFontProvider()
633
- super.init(frame: .zero)
634
- commonInit()
635
- }
636
-
637
- required public init?(coder aDecoder: NSCoder) {
638
- self.imageProvider = BundleImageProvider(bundle: Bundle.main, searchPath: nil)
639
- self.textProvider = DefaultTextProvider()
640
- self.fontProvider = DefaultFontProvider()
641
- super.init(coder: aDecoder)
642
- commonInit()
643
- }
644
-
645
- // MARK: - Public (UIView Overrides)
646
-
647
- override public var intrinsicContentSize: CGSize {
648
- if let animation = animation {
649
- return animation.bounds.size
650
- }
651
- return .zero
652
- }
653
-
654
- override func layoutAnimation() {
655
- guard let animation = animation, let animationLayer = animationLayer else { return }
656
- var position = animation.bounds.center
657
- let xform: CATransform3D
658
- var shouldForceUpdates: Bool = false
659
-
660
- if let viewportFrame = self.viewportFrame {
661
- shouldForceUpdates = self.contentMode == .redraw
662
-
663
- let compAspect = viewportFrame.size.width / viewportFrame.size.height
664
- let viewAspect = bounds.size.width / bounds.size.height
665
- let dominantDimension = compAspect > viewAspect ? bounds.size.width : bounds.size.height
666
- let compDimension = compAspect > viewAspect ? viewportFrame.size.width : viewportFrame.size.height
667
- let scale = dominantDimension / compDimension
668
-
669
- let viewportOffset = animation.bounds.center - viewportFrame.center
670
- xform = CATransform3DTranslate(CATransform3DMakeScale(scale, scale, 1), viewportOffset.x, viewportOffset.y, 0)
671
- position = bounds.center
672
- } else {
673
- switch contentMode {
674
- case .scaleToFill:
675
- position = bounds.center
676
- xform = CATransform3DMakeScale(bounds.size.width / animation.size.width,
677
- bounds.size.height / animation.size.height,
678
- 1);
679
- case .scaleAspectFit:
680
- position = bounds.center
681
- let compAspect = animation.size.width / animation.size.height
682
- let viewAspect = bounds.size.width / bounds.size.height
683
- let dominantDimension = compAspect > viewAspect ? bounds.size.width : bounds.size.height
684
- let compDimension = compAspect > viewAspect ? animation.size.width : animation.size.height
685
- let scale = dominantDimension / compDimension
686
- xform = CATransform3DMakeScale(scale, scale, 1)
687
- case .scaleAspectFill:
688
- position = bounds.center
689
- let compAspect = animation.size.width / animation.size.height
690
- let viewAspect = bounds.size.width / bounds.size.height
691
- let scaleWidth = compAspect < viewAspect
692
- let dominantDimension = scaleWidth ? bounds.size.width : bounds.size.height
693
- let compDimension = scaleWidth ? animation.size.width : animation.size.height
694
- let scale = dominantDimension / compDimension
695
- xform = CATransform3DMakeScale(scale, scale, 1)
696
- case .redraw:
697
- shouldForceUpdates = true
698
- xform = CATransform3DIdentity
699
- case .center:
700
- position = bounds.center
701
- xform = CATransform3DIdentity
702
- case .top:
703
- position.x = bounds.center.x
704
- xform = CATransform3DIdentity
705
- case .bottom:
706
- position.x = bounds.center.x
707
- position.y = bounds.maxY - animation.bounds.midY
708
- xform = CATransform3DIdentity
709
- case .left:
710
- position.y = bounds.center.y
711
- xform = CATransform3DIdentity
712
- case .right:
713
- position.y = bounds.center.y
714
- position.x = bounds.maxX - animation.bounds.midX
715
- xform = CATransform3DIdentity
716
- case .topLeft:
717
- xform = CATransform3DIdentity
718
- case .topRight:
719
- position.x = bounds.maxX - animation.bounds.midX
720
- xform = CATransform3DIdentity
721
- case .bottomLeft:
722
- position.y = bounds.maxY - animation.bounds.midY
723
- xform = CATransform3DIdentity
724
- case .bottomRight:
725
- position.x = bounds.maxX - animation.bounds.midX
726
- position.y = bounds.maxY - animation.bounds.midY
727
- xform = CATransform3DIdentity
728
-
729
- #if os(iOS) || os(tvOS)
730
- @unknown default:
731
- print("unsupported contentMode: \(contentMode.rawValue); please update lottie-ios")
732
- xform = CATransform3DIdentity
733
- #endif
734
- }
735
- }
736
-
737
- /*
738
- UIView Animation does not implicitly set CAAnimation time or timing fuctions.
739
- If layout is changed in an animation we must get the current animation duration
740
- and timing function and then manually create a CAAnimation to match the UIView animation.
741
- If layout is changed without animation, explicitly set animation duration to 0.0
742
- inside CATransaction to avoid unwanted artifacts.
743
- */
744
- /// Check if any animation exist on the view's layer, and match it.
745
- if let key = viewLayer?.animationKeys()?.first, let animation = viewLayer?.animation(forKey: key) {
746
- // The layout is happening within an animation block. Grab the animation data.
747
-
748
- let positionKey = "LayoutPositionAnimation"
749
- let transformKey = "LayoutTransformAnimation"
750
- animationLayer.removeAnimation(forKey: positionKey)
751
- animationLayer.removeAnimation(forKey: transformKey)
752
-
753
- let positionAnimation = animation.copy() as? CABasicAnimation ?? CABasicAnimation(keyPath: "position")
754
- positionAnimation.keyPath = "position"
755
- positionAnimation.isAdditive = false
756
- positionAnimation.fromValue = animationLayer.position
757
- positionAnimation.toValue = position
758
- positionAnimation.isRemovedOnCompletion = true
759
-
760
- let xformAnimation = animation.copy() as? CABasicAnimation ?? CABasicAnimation(keyPath: "transform")
761
- xformAnimation.keyPath = "transform"
762
- xformAnimation.isAdditive = false
763
- xformAnimation.fromValue = animationLayer.transform
764
- xformAnimation.toValue = xform
765
- xformAnimation.isRemovedOnCompletion = true
766
-
767
- animationLayer.position = position
768
- animationLayer.transform = xform
769
- #if os(OSX)
770
- animationLayer.anchorPoint = layer?.anchorPoint ?? CGPoint.zero
771
- #else
772
- animationLayer.anchorPoint = layer.anchorPoint
773
- #endif
774
- animationLayer.add(positionAnimation, forKey: positionKey)
775
- animationLayer.add(xformAnimation, forKey: transformKey)
776
- } else {
777
- CATransaction.begin()
778
- CATransaction.setAnimationDuration(0.0)
779
- CATransaction.setAnimationTimingFunction(CAMediaTimingFunction(name: .linear))
780
- animationLayer.position = position
781
- animationLayer.transform = xform
782
- CATransaction.commit()
783
- }
784
-
785
- if shouldForceUpdates {
786
- animationLayer.forceDisplayUpdate()
787
- }
788
- }
789
-
790
- // MARK: - Private (Properties)
791
-
792
-
793
- var animationLayer: AnimationContainer? = nil
794
-
795
- fileprivate var animationContext: AnimationContext?
796
- static private let animationName: String = "Lottie"
797
- fileprivate var activeAnimationName: String = AnimationView.animationName
798
- fileprivate var animationID: Int = 0
799
-
800
- // MARK: - Private (Building Animation View)
801
-
802
- fileprivate func makeAnimationLayer() {
803
-
804
- /// Remove current animation if any
805
- removeCurrentAnimation()
806
-
807
- if let oldAnimation = self.animationLayer {
808
- oldAnimation.removeFromSuperlayer()
809
- }
810
-
811
- invalidateIntrinsicContentSize()
812
-
813
- guard let animation = animation else {
814
- return
815
- }
816
-
817
- let animationLayer = AnimationContainer(animation: animation, imageProvider: imageProvider, textProvider: textProvider, fontProvider: fontProvider)
818
- animationLayer.renderScale = self.screenScale
819
- viewLayer?.addSublayer(animationLayer)
820
- self.animationLayer = animationLayer
821
- reloadImages()
822
- animationLayer.setNeedsDisplay()
823
- setNeedsLayout()
824
- currentFrame = CGFloat(animation.startFrame)
825
- }
826
-
827
- func updateRasterizationState() {
828
- if isAnimationPlaying {
829
- animationLayer?.shouldRasterize = false
830
- } else {
831
- animationLayer?.shouldRasterize = shouldRasterizeWhenIdle
832
- }
833
- }
834
-
835
- // MARK: - Private (Animation Playback)
836
-
837
- /// Updates the animation frame. Does not affect any current animations
838
- func updateAnimationFrame(_ newFrame: CGFloat) {
839
- CATransaction.begin()
840
- CATransaction.setCompletionBlock {
841
- self.animationLayer?.forceDisplayUpdate()
842
- }
843
- CATransaction.setDisableActions(true)
844
- animationLayer?.currentFrame = newFrame
845
- CATransaction.commit()
846
- }
847
-
848
- @objc override func animationWillMoveToBackground() {
849
- updateAnimationForBackgroundState()
850
- }
851
-
852
- @objc override func animationWillEnterForeground() {
853
- updateAnimationForForegroundState()
854
- }
855
-
856
- override func animationMovedToWindow() {
857
- /// Don't update any state if the `superview` is `nil`
858
- /// When A viewA owns superViewB, it removes the superViewB from the window. At this point, viewA still owns superViewB and triggers the viewA method: -didmovetowindow
859
- guard superview != nil else { return }
860
-
861
- if window != nil {
862
- updateAnimationForForegroundState()
863
- } else {
864
- updateAnimationForBackgroundState()
865
- }
866
- }
867
-
868
- fileprivate func updateAnimationForBackgroundState() {
869
- if let currentContext = animationContext {
870
- switch backgroundBehavior {
871
- case .stop:
872
- removeCurrentAnimation()
873
- updateAnimationFrame(currentContext.playFrom)
874
- case .pause:
875
- removeCurrentAnimation()
876
- case .pauseAndRestore:
877
- currentContext.closure.ignoreDelegate = true
878
- removeCurrentAnimation()
879
- /// Keep the stale context around for when the app enters the foreground.
880
- self.animationContext = currentContext
881
- case .forceFinish:
882
- removeCurrentAnimation()
883
- updateAnimationFrame(currentContext.playTo)
884
- }
885
- }
886
- }
887
-
888
- fileprivate var waitingToPlayAnimation: Bool = false
889
- fileprivate func updateAnimationForForegroundState() {
890
- if let currentContext = animationContext {
891
- if waitingToPlayAnimation {
892
- waitingToPlayAnimation = false
893
- self.addNewAnimationForContext(currentContext)
894
- } else if backgroundBehavior == .pauseAndRestore {
895
- /// Restore animation from saved state
896
- updateInFlightAnimation()
897
- }
898
- }
899
- }
900
-
901
- /// Stops the current in flight animation and freezes the animation in its current state.
902
- fileprivate func removeCurrentAnimation() {
903
- guard animationContext != nil else { return }
904
- let pauseFrame = realtimeAnimationFrame
905
- animationLayer?.removeAnimation(forKey: activeAnimationName)
906
- updateAnimationFrame(pauseFrame)
907
- self.animationContext = nil
908
- }
909
-
910
- /// Updates an in flight animation.
911
- fileprivate func updateInFlightAnimation() {
912
- guard let animationContext = animationContext else { return }
913
-
914
- guard animationContext.closure.animationState != .complete else {
915
- // Tried to re-add an already completed animation. Cancel.
916
- self.animationContext = nil
917
- return
918
- }
919
-
920
- /// Tell existing context to ignore its closure
921
- animationContext.closure.ignoreDelegate = true
922
-
923
- /// Make a new context, stealing the completion block from the previous.
924
- let newContext = AnimationContext(playFrom: animationContext.playFrom,
925
- playTo: animationContext.playTo,
926
- closure: animationContext.closure.completionBlock)
927
-
928
- /// Remove current animation, and freeze the current frame.
929
- let pauseFrame = realtimeAnimationFrame
930
- animationLayer?.removeAnimation(forKey: activeAnimationName)
931
- animationLayer?.currentFrame = pauseFrame
932
-
933
- addNewAnimationForContext(newContext)
934
- }
935
-
936
- /// Adds animation to animation layer and sets the delegate. If animation layer or animation are nil, exits.
937
- fileprivate func addNewAnimationForContext(_ animationContext: AnimationContext) {
938
- guard let animationlayer = animationLayer, let animation = animation else {
939
- return
940
- }
941
-
942
- self.animationContext = animationContext
943
-
944
- guard self.window != nil else { waitingToPlayAnimation = true; return }
945
-
946
- animationID = animationID + 1
947
- activeAnimationName = AnimationView.animationName + String(animationID)
948
-
949
- /// At this point there is no animation on animationLayer and its state is set.
950
-
951
- let framerate = animation.framerate
952
-
953
- let playFrom = animationContext.playFrom.clamp(animation.startFrame, animation.endFrame)
954
- let playTo = animationContext.playTo.clamp(animation.startFrame, animation.endFrame)
955
-
956
- let duration = ((max(playFrom, playTo) - min(playFrom, playTo)) / CGFloat(framerate))
957
-
958
- let playingForward: Bool =
959
- ((animationSpeed > 0 && playFrom < playTo) ||
960
- (animationSpeed < 0 && playTo < playFrom))
961
-
962
- var startFrame = currentFrame.clamp(min(playFrom, playTo), max(playFrom, playTo))
963
- if startFrame == playTo {
964
- startFrame = playFrom
965
- }
966
-
967
- let timeOffset: TimeInterval = playingForward ?
968
- Double(startFrame - min(playFrom, playTo)) / framerate :
969
- Double(max(playFrom, playTo) - startFrame) / framerate
970
-
971
- let layerAnimation = CABasicAnimation(keyPath: "currentFrame")
972
- layerAnimation.fromValue = playFrom
973
- layerAnimation.toValue = playTo
974
- layerAnimation.speed = Float(animationSpeed)
975
- layerAnimation.duration = TimeInterval(duration)
976
- layerAnimation.fillMode = CAMediaTimingFillMode.both
977
-
978
- switch loopMode {
979
- case .playOnce:
980
- layerAnimation.repeatCount = 1
981
- case .loop:
982
- layerAnimation.repeatCount = HUGE
983
- case .autoReverse:
984
- layerAnimation.repeatCount = HUGE
985
- layerAnimation.autoreverses = true
986
- case let .repeat(amount):
987
- layerAnimation.repeatCount = amount
988
- case let .repeatBackwards(amount):
989
- layerAnimation.repeatCount = amount
990
- layerAnimation.autoreverses = true
991
- }
992
-
993
- layerAnimation.isRemovedOnCompletion = false
994
- if timeOffset != 0 {
995
- let currentLayerTime = viewLayer?.convertTime(CACurrentMediaTime(), from: nil) ?? 0
996
- layerAnimation.beginTime = currentLayerTime - (timeOffset * 1 / Double(abs(animationSpeed)))
997
- }
998
- layerAnimation.delegate = animationContext.closure
999
- animationContext.closure.animationLayer = animationlayer
1000
- animationContext.closure.animationKey = activeAnimationName
1001
-
1002
- animationlayer.add(layerAnimation, forKey: activeAnimationName)
1003
- updateRasterizationState()
1004
- }
1005
-
1006
- }