ActivityIndicatorAnimationBallGridBeat.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 ActivityIndicatorAnimationBallGridBeat: 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 circleSpacing: CGFloat = 2
  12. let circleSize = (size.width - circleSpacing * 2) / 3
  13. let x = (layer.bounds.size.width - size.width) / 2
  14. let y = (layer.bounds.size.height - size.height) / 2
  15. let durations = [0.96, 0.93, 1.19, 1.13, 1.34, 0.94, 1.2, 0.82, 1.19]
  16. let beginTime = layer.currentMediaTime
  17. let beginTimes = [0.36, 0.4, 0.68, 0.41, 0.71, -0.15, -0.12, 0.01, 0.32]
  18. let animation = defaultAnimation
  19. // Draw circles
  20. for i in 0 ..< 3 {
  21. for j in 0 ..< 3 {
  22. let circle = ActivityIndicatorShape.circle.makeLayer(size: CGSize(width: circleSize, height: circleSize), color: color)
  23. let frame = CGRect(x: x + circleSize * CGFloat(j) + circleSpacing * CGFloat(j),
  24. y: y + circleSize * CGFloat(i) + circleSpacing * CGFloat(i),
  25. width: circleSize,
  26. height: circleSize)
  27. animation.duration = durations[3 * i + j]
  28. animation.beginTime = beginTime + beginTimes[3 * i + j]
  29. circle.frame = frame
  30. circle.add(animation, forKey: "animation")
  31. layer.addSublayer(circle)
  32. }
  33. }
  34. }
  35. }
  36. // MARK: - Setup
  37. private extension ActivityIndicatorAnimationBallGridBeat {
  38. var defaultAnimation: CAKeyframeAnimation {
  39. let animation = CAKeyframeAnimation(keyPath: .opacity)
  40. animation.keyTimes = [0, 0.5, 1]
  41. animation.timingFunctionsType = [timingFunction, timingFunction]
  42. animation.values = [1, 0.7, 1]
  43. animation.repeatCount = .infinity
  44. animation.isRemovedOnCompletion = false
  45. return animation
  46. }
  47. }