ActivityIndicatorAnimationLineScalePulseOut.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // Created by Tom Baranes on 21/08/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. public class ActivityIndicatorAnimationLineScalePulseOut: 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 lineSize = size.width / 9
  12. let x = (layer.bounds.size.width - size.width) / 2
  13. let y = (layer.bounds.size.height - size.height) / 2
  14. let beginTime = layer.currentMediaTime
  15. let beginTimes = [0.4, 0.2, 0, 0.2, 0.4]
  16. let animation = defaultAnimation
  17. // Draw lines
  18. for i in 0 ..< 5 {
  19. let line = ActivityIndicatorShape.line.makeLayer(size: CGSize(width: lineSize, height: size.height), color: color)
  20. let frame = CGRect(x: x + lineSize * 2 * CGFloat(i),
  21. y: y,
  22. width: lineSize,
  23. height: size.height)
  24. animation.beginTime = beginTime + beginTimes[i]
  25. line.frame = frame
  26. line.add(animation, forKey: "animation")
  27. layer.addSublayer(line)
  28. }
  29. }
  30. }
  31. // MARK: - Setup
  32. private extension ActivityIndicatorAnimationLineScalePulseOut {
  33. var defaultAnimation: CAKeyframeAnimation {
  34. let timingFunction = CAMediaTimingFunction(controlPoints: 0.85, 0.25, 0.37, 0.85)
  35. let animation = CAKeyframeAnimation(keyPath: .scaleY)
  36. animation.keyTimes = [0, 0.5, 1]
  37. animation.timingFunctions = [timingFunction, timingFunction]
  38. animation.values = [1, 0.4, 1]
  39. animation.duration = duration
  40. animation.repeatCount = .infinity
  41. animation.isRemovedOnCompletion = false
  42. return animation
  43. }
  44. }