ActivityIndicatorAnimationLineScale.swift 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 ActivityIndicatorAnimationLineScale: ActivityIndicatorAnimating {
  7. // MARK: Properties
  8. fileprivate let duration: CFTimeInterval = 1
  9. fileprivate let timingFunction = CAMediaTimingFunction(controlPoints: 0.2, 0.68, 0.18, 1.08)
  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 beginTime = layer.currentMediaTime
  16. let beginTimes = [0.1, 0.2, 0.3, 0.4, 0.5]
  17. let animation = defaultAnimation
  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), y: y, width: lineSize, height: size.height)
  21. animation.beginTime = beginTime + beginTimes[i]
  22. line.frame = frame
  23. line.add(animation, forKey: "animation")
  24. layer.addSublayer(line)
  25. }
  26. }
  27. }
  28. // MARK: - Setup
  29. private extension ActivityIndicatorAnimationLineScale {
  30. var defaultAnimation: CAKeyframeAnimation {
  31. let animation = CAKeyframeAnimation(keyPath: .scaleY)
  32. animation.keyTimes = [0, 0.5, 1]
  33. animation.timingFunctions = [timingFunction, timingFunction]
  34. animation.values = [1, 0.4, 1]
  35. animation.duration = duration
  36. animation.repeatCount = .infinity
  37. animation.isRemovedOnCompletion = false
  38. return animation
  39. }
  40. }