ActivityIndicatorAnimationBallSpinFadeLoader.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ActivityIndicatorAnimationBallSpinFadeLoader: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let duration: CFTimeInterval = 1
  9. // MARK: ActivityIndicatorAnimating
  10. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  11. let circleSpacing: CGFloat = -2
  12. let circleSize = (size.width - 4 * circleSpacing) / 5
  13. let x = (layer.bounds.size.width - size.width) / 2
  14. let y = (layer.bounds.size.height - size.height) / 2
  15. let beginTime = layer.currentMediaTime
  16. let beginTimes: [CFTimeInterval] = [0, 0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84]
  17. // Draw circles
  18. let animation = defaultAnimation
  19. for i in 0 ..< 8 {
  20. let circle = makeCircleLayer(angle: CGFloat.pi / 4 * CGFloat(i),
  21. size: circleSize,
  22. origin: CGPoint(x: x, y: y),
  23. containerSize: size,
  24. color: color)
  25. animation.beginTime = beginTime + beginTimes[i]
  26. circle.add(animation, forKey: "animation")
  27. layer.addSublayer(circle)
  28. }
  29. }
  30. func makeCircleLayer(angle: CGFloat, size: CGFloat, origin: CGPoint, containerSize: CGSize, color: UIColor) -> CALayer {
  31. let radius = containerSize.width / 2 - size / 2
  32. let circle = ActivityIndicatorShape.circle.makeLayer(size: CGSize(width: size, height: size), color: color)
  33. let frame = CGRect(
  34. x: origin.x + radius * (cos(angle) + 1),
  35. y: origin.y + radius * (sin(angle) + 1),
  36. width: size,
  37. height: size)
  38. circle.frame = frame
  39. return circle
  40. }
  41. }
  42. // MARK: - Setup
  43. private extension ActivityIndicatorAnimationBallSpinFadeLoader {
  44. var defaultAnimation: CAAnimationGroup {
  45. let animation = CAAnimationGroup()
  46. animation.animations = [scaleAnimation, opacityAnimation]
  47. animation.timingFunctionType = .linear
  48. animation.duration = duration
  49. animation.repeatCount = .infinity
  50. animation.isRemovedOnCompletion = false
  51. return animation
  52. }
  53. var scaleAnimation: CAKeyframeAnimation {
  54. let scaleAnimation = CAKeyframeAnimation(keyPath: .scale)
  55. scaleAnimation.keyTimes = [0, 0.5, 1]
  56. scaleAnimation.values = [1, 0.4, 1]
  57. scaleAnimation.duration = duration
  58. return scaleAnimation
  59. }
  60. var opacityAnimation: CAKeyframeAnimation {
  61. let opacityAnimation = CAKeyframeAnimation(keyPath: .opacity)
  62. opacityAnimation.keyTimes = [0, 0.5, 1]
  63. opacityAnimation.values = [1, 0.3, 1]
  64. opacityAnimation.duration = duration
  65. return opacityAnimation
  66. }
  67. }