ActivityIndicatorAnimationBallPulse.swift 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 ActivityIndicatorAnimationBallPulse: ActivityIndicatorAnimating {
  7. // MARK: ActivityIndicatorAnimating
  8. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  9. let circleSpacing: CGFloat = 2
  10. let circleSize: CGFloat = (size.width - 2 * circleSpacing) / 3
  11. let x: CGFloat = (layer.bounds.size.width - size.width) / 2
  12. let y: CGFloat = (layer.bounds.size.height - circleSize) / 2
  13. let beginTime = layer.currentMediaTime
  14. let beginTimes: [CFTimeInterval] = [0.12, 0.24, 0.36]
  15. let animation = defaultAnimation
  16. // Draw circles
  17. for i in 0 ..< 3 {
  18. let circle = ActivityIndicatorShape.circle.makeLayer(size: CGSize(width: circleSize, height: circleSize), color: color)
  19. let frame = CGRect(x: x + circleSize * CGFloat(i) + circleSpacing * CGFloat(i),
  20. y: y,
  21. width: circleSize,
  22. height: circleSize)
  23. animation.beginTime = beginTime + beginTimes[i]
  24. circle.frame = frame
  25. circle.add(animation, forKey: "animation")
  26. layer.addSublayer(circle)
  27. }
  28. }
  29. }
  30. // MARK: - Setup
  31. private extension ActivityIndicatorAnimationBallPulse {
  32. var defaultAnimation: CAKeyframeAnimation {
  33. let duration: CFTimeInterval = 0.75
  34. let timingFunction = CAMediaTimingFunction(controlPoints: 0.2, 0.68, 0.18, 1.08)
  35. let animation = CAKeyframeAnimation(keyPath: .scale)
  36. animation.keyTimes = [0, 0.3, 1]
  37. animation.timingFunctions = [timingFunction, timingFunction]
  38. animation.values = [1, 0.3, 1]
  39. animation.duration = duration
  40. animation.repeatCount = .infinity
  41. animation.isRemovedOnCompletion = false
  42. return animation
  43. }
  44. }