ActivityIndicatorAnimationAudioEqualizer.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Created by Tom Baranes on 23/08/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public class ActivityIndicatorAnimationAudioEqualizer: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate var size: CGSize = .zero
  9. fileprivate var lineSize: CGFloat = 0
  10. // MARK: ActivityIndicatorAnimating
  11. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  12. self.size = size
  13. lineSize = size.width / 9
  14. let x = (layer.bounds.size.width - lineSize * 7) / 2
  15. let y = (layer.bounds.size.height - size.height) / 2
  16. let duration: [CFTimeInterval] = [4.3, 2.5, 1.7, 3.1]
  17. let values = [0, 0.7, 0.4, 0.05, 0.95, 0.3, 0.9, 0.4, 0.15, 0.18, 0.75, 0.01]
  18. // Draw lines
  19. for i in 0 ..< 4 {
  20. let animation = makeAnimation(duration: duration[i], values: values)
  21. let line = ActivityIndicatorShape.line.makeLayer(size: CGSize(width: lineSize, height: size.height), color: color)
  22. let frame = CGRect(x: x + lineSize * 2 * CGFloat(i),
  23. y: y,
  24. width: lineSize,
  25. height: size.height)
  26. line.frame = frame
  27. line.add(animation, forKey: "animation")
  28. layer.addSublayer(line)
  29. }
  30. }
  31. }
  32. // MARK: - Setup
  33. private extension ActivityIndicatorAnimationAudioEqualizer {
  34. func makeAnimation(duration: CFTimeInterval, values: [Double]) -> CAKeyframeAnimation {
  35. let animation = CAKeyframeAnimation()
  36. animation.keyPath = "path"
  37. animation.isAdditive = true
  38. animation.values = []
  39. for j in 0..<values.count {
  40. let heightFactor = values[j]
  41. let height = size.height * CGFloat(heightFactor)
  42. let point = CGPoint(x: 0, y: size.height - height)
  43. let path = UIBezierPath(rect: CGRect(origin: point, size: CGSize(width: lineSize, height: height)))
  44. animation.values?.append(path.cgPath)
  45. }
  46. animation.duration = duration
  47. animation.repeatCount = .infinity
  48. animation.isRemovedOnCompletion = false
  49. return animation
  50. }
  51. }