ActivityIndicatorAnimationLineScaleParty.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //
  2. // Created by Tom Baranes on 23/08/16.
  3. // Copyright (c) 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public class ActivityIndicatorAnimationLineScaleParty: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let timingFunction: TimingFunctionType = .default
  9. // MARK: ActivityIndicatorAnimating
  10. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  11. let lineSize = size.width / 7
  12. let x = (layer.bounds.size.width - size.width) / 2
  13. let y = (layer.bounds.size.height - size.height) / 2
  14. let durations: [CFTimeInterval] = [1.26, 0.43, 1.01, 0.73]
  15. let beginTime = layer.currentMediaTime
  16. let beginTimes: [CFTimeInterval] = [0.77, 0.29, 0.28, 0.74]
  17. let animation = defaultAnimation
  18. // Animation
  19. for i in 0..<4 {
  20. let line = ActivityIndicatorShape.line.makeLayer(size: CGSize(width: lineSize, height: size.height), color: color)
  21. let frame = CGRect(x: x + lineSize * 2 * CGFloat(i), y: y, width: lineSize, height: size.height)
  22. animation.beginTime = beginTime + beginTimes[i]
  23. animation.duration = durations[i]
  24. line.frame = frame
  25. line.add(animation, forKey: "animation")
  26. layer.addSublayer(line)
  27. }
  28. }
  29. }
  30. // MARK: - Setup
  31. private extension ActivityIndicatorAnimationLineScaleParty {
  32. var defaultAnimation: CAKeyframeAnimation {
  33. let animation = CAKeyframeAnimation(keyPath: .scale)
  34. animation.keyTimes = [0, 0.5, 1]
  35. animation.timingFunctionsType = [timingFunction, timingFunction]
  36. animation.values = [1, 0.5, 1]
  37. animation.repeatCount = .infinity
  38. animation.isRemovedOnCompletion = false
  39. return animation
  40. }
  41. }