ActivityIndicatorAnimatable.swift 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //
  2. // Created by Tom Baranes on 21/08/16.
  3. // Copyright © 2016 IBAnimatable. All rights reserved.
  4. //
  5. import UIKit
  6. /// Protocol for activity indicator view.
  7. public protocol ActivityIndicatorAnimatable: class {
  8. /// Animation type
  9. var animationType: ActivityIndicatorType { get set }
  10. /// Color of the indicator
  11. var color: UIColor { get set }
  12. /// Specify whether hide the indicator when the animation stopped
  13. var hidesWhenStopped: Bool { get set }
  14. /// Animating status
  15. var isAnimating: Bool { get set }
  16. }
  17. public extension ActivityIndicatorAnimatable where Self: UIView {
  18. /// Start animating the activity indicator
  19. func startAnimating() {
  20. isHidden = false
  21. configureLayer()
  22. isAnimating = true
  23. }
  24. /// Stop animating the activity indicator
  25. func stopAnimating() {
  26. layer.sublayers = nil
  27. isAnimating = false
  28. if hidesWhenStopped {
  29. isHidden = true
  30. }
  31. }
  32. }
  33. private extension ActivityIndicatorAnimatable where Self: UIView {
  34. func configureLayer() {
  35. guard layer.sublayers == nil else {
  36. return
  37. }
  38. if case .none = animationType {
  39. return
  40. }
  41. animationType.configureAnimation(in: layer, size: bounds.size, color: color)
  42. layer.speed = 1
  43. }
  44. }