ActivityIndicatorAnimationLineScalePulseOutRapid.swift 1.7 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 ActivityIndicatorAnimationLineScalePulseOutRapid: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let duration: CFTimeInterval = 0.9
  9. fileprivate let timingFunction = CAMediaTimingFunction(controlPoints: 0.11, 0.49, 0.38, 0.78)
  10. // MARK: ActivityIndicatorAnimating
  11. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  12. let lineSize = size.width / 9
  13. let x = (layer.bounds.size.width - size.width) / 2
  14. let y = (layer.bounds.size.height - size.height) / 2
  15. let beginTimes = [0.5, 0.25, 0, 0.25, 0.5]
  16. let animation = defaultAnimation
  17. for i in 0 ..< 5 {
  18. let line = ActivityIndicatorShape.line.makeLayer(size: CGSize(width: lineSize, height: size.height), color: color)
  19. let frame = CGRect(x: x + lineSize * 2 * CGFloat(i),
  20. y: y,
  21. width: lineSize,
  22. height: size.height)
  23. animation.beginTime = layer.currentMediaTime + beginTimes[i]
  24. line.frame = frame
  25. line.add(animation, forKey: "animation")
  26. layer.addSublayer(line)
  27. }
  28. }
  29. }
  30. // MARK: - Setup
  31. private extension ActivityIndicatorAnimationLineScalePulseOutRapid {
  32. var defaultAnimation: CAKeyframeAnimation {
  33. let animation = CAKeyframeAnimation(keyPath: .scaleY)
  34. animation.keyTimes = [0, 0.8, 0.9]
  35. animation.timingFunctions = [timingFunction, timingFunction]
  36. animation.beginTime = CACurrentMediaTime()
  37. animation.values = [1, 0.3, 1]
  38. animation.duration = duration
  39. animation.repeatCount = .infinity
  40. animation.isRemovedOnCompletion = false
  41. return animation
  42. }
  43. }