ActivityIndicatorAnimationLineSpinFadeLoader.swift 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ActivityIndicatorAnimationLineSpinFadeLoader: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let duration: CFTimeInterval = 1.2
  9. fileprivate let timingFunction: TimingFunctionType = .easeInOut
  10. // MARK: ActivityIndicatorAnimating
  11. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  12. let lineSpacing: CGFloat = 2
  13. let lineSize = CGSize(width: (size.width - 4 * lineSpacing) / 5, height: (size.height - 2 * lineSpacing) / 3)
  14. let x = (layer.bounds.size.width - size.width) / 2
  15. let y = (layer.bounds.size.height - size.height) / 2
  16. let beginTime = layer.currentMediaTime
  17. let beginTimes: [CFTimeInterval] = [0.12, 0.24, 0.36, 0.48, 0.6, 0.72, 0.84, 0.96]
  18. let animation = defaultAnimation
  19. for i in 0 ..< 8 {
  20. let line = makeLineLayer(angle: CGFloat.pi / 4 * CGFloat(i),
  21. size: lineSize,
  22. origin: CGPoint(x: x, y: y),
  23. containerSize: size,
  24. color: color)
  25. animation.beginTime = beginTime + beginTimes[i]
  26. line.add(animation, forKey: "animation")
  27. layer.addSublayer(line)
  28. }
  29. }
  30. }
  31. // MARK: - Setup
  32. private extension ActivityIndicatorAnimationLineSpinFadeLoader {
  33. var defaultAnimation: CAKeyframeAnimation {
  34. let animation = CAKeyframeAnimation(keyPath: .opacity)
  35. animation.keyTimes = [0, 0.5, 1]
  36. animation.timingFunctionsType = [timingFunction, timingFunction]
  37. animation.values = [1, 0.3, 1]
  38. animation.duration = duration
  39. animation.repeatCount = .infinity
  40. animation.isRemovedOnCompletion = false
  41. return animation
  42. }
  43. func makeLineLayer(angle: CGFloat, size: CGSize, origin: CGPoint, containerSize: CGSize, color: UIColor) -> CALayer {
  44. let radius = containerSize.width / 2 - max(size.width, size.height) / 2
  45. let lineContainerSize = CGSize(width: max(size.width, size.height), height: max(size.width, size.height))
  46. let lineContainer = CALayer()
  47. let lineContainerFrame = CGRect(
  48. x: origin.x + radius * (cos(angle) + 1),
  49. y: origin.y + radius * (sin(angle) + 1),
  50. width: lineContainerSize.width,
  51. height: lineContainerSize.height)
  52. let line = ActivityIndicatorShape.line.makeLayer(size: size, color: color)
  53. let lineFrame = CGRect(
  54. x: (lineContainerSize.width - size.width) / 2,
  55. y: (lineContainerSize.height - size.height) / 2,
  56. width: size.width,
  57. height: size.height)
  58. lineContainer.frame = lineContainerFrame
  59. line.frame = lineFrame
  60. lineContainer.addSublayer(line)
  61. lineContainer.sublayerTransform = CATransform3DMakeRotation(CGFloat.pi / 2 + angle, 0, 0, 1)
  62. return lineContainer
  63. }
  64. }