ActivityIndicatorAnimationBallScale.swift 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 ActivityIndicatorAnimationBallScale: 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 circle = ActivityIndicatorShape.circle.makeLayer(size: size, color: color)
  12. circle.frame = CGRect(x: (layer.bounds.size.width - size.width) / 2,
  13. y: (layer.bounds.size.height - size.height) / 2,
  14. width: size.width,
  15. height: size.height)
  16. circle.add(animation, forKey: "animation")
  17. layer.addSublayer(circle)
  18. }
  19. }
  20. // MARK: - Setup
  21. private extension ActivityIndicatorAnimationBallScale {
  22. var animation: CAAnimationGroup {
  23. let animation = CAAnimationGroup()
  24. animation.animations = [scaleAnimation, opacityAnimation]
  25. animation.timingFunctionType = .easeInOut
  26. animation.duration = duration
  27. animation.repeatCount = .infinity
  28. animation.isRemovedOnCompletion = false
  29. return animation
  30. }
  31. var scaleAnimation: CABasicAnimation {
  32. let scaleAnimation = CABasicAnimation(keyPath: .scale)
  33. scaleAnimation.duration = duration
  34. scaleAnimation.fromValue = 0
  35. scaleAnimation.toValue = 1
  36. return scaleAnimation
  37. }
  38. var opacityAnimation: CABasicAnimation {
  39. let opacityAnimation = CABasicAnimation(keyPath: .opacity)
  40. opacityAnimation.duration = duration
  41. opacityAnimation.fromValue = 1
  42. opacityAnimation.toValue = 0
  43. return opacityAnimation
  44. }
  45. }