ActivityIndicatorAnimationBallScaleMultiple.swift 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 ActivityIndicatorAnimationBallScaleMultiple: 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 beginTime = layer.currentMediaTime
  12. let beginTimes = [0, 0.2, 0.4]
  13. let animation = defaultAnimation
  14. for i in 0 ..< 3 {
  15. let circle = ActivityIndicatorShape.circle.makeLayer(size: size, color: color)
  16. let frame = CGRect(x: (layer.bounds.size.width - size.width) / 2,
  17. y: (layer.bounds.size.height - size.height) / 2,
  18. width: size.width,
  19. height: size.height)
  20. animation.beginTime = beginTime + beginTimes[i]
  21. circle.frame = frame
  22. circle.opacity = 0
  23. circle.add(animation, forKey: "animation")
  24. layer.addSublayer(circle)
  25. }
  26. }
  27. }
  28. // MARK: - Setup
  29. private extension ActivityIndicatorAnimationBallScaleMultiple {
  30. var defaultAnimation: CAAnimationGroup {
  31. let animation = CAAnimationGroup()
  32. animation.animations = [scaleAnimation, opacityAnimation]
  33. animation.timingFunctionType = .linear
  34. animation.duration = duration
  35. animation.repeatCount = .infinity
  36. animation.isRemovedOnCompletion = false
  37. return animation
  38. }
  39. var scaleAnimation: CABasicAnimation {
  40. let scaleAnimation = CABasicAnimation(keyPath: .scale)
  41. scaleAnimation.duration = duration
  42. scaleAnimation.fromValue = 0
  43. scaleAnimation.toValue = 1
  44. return scaleAnimation
  45. }
  46. var opacityAnimation: CAKeyframeAnimation {
  47. let opacityAnimation = CAKeyframeAnimation(keyPath: .opacity)
  48. opacityAnimation.duration = duration
  49. opacityAnimation.keyTimes = [0, 0.05, 1]
  50. opacityAnimation.values = [0, 1, 0]
  51. return opacityAnimation
  52. }
  53. }