ActivityIndicatorAnimationGear.swift 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // ActivityIndicatorAnimationGear.swift
  3. // IBAnimatable
  4. //
  5. // Created by phimage on 02/05/2018.
  6. // Copyright © 2018 IBAnimatable. All rights reserved.
  7. //
  8. import UIKit
  9. public class ActivityIndicatorAnimationGear: ActivityIndicatorAnimating {
  10. // MARK: Properties
  11. fileprivate let duration: CFTimeInterval = 3
  12. // MARK: ActivityIndicatorAnimating
  13. public func configureAnimation(in layer: CALayer, size: CGSize, color: UIColor) {
  14. let x = (layer.bounds.size.width - size.width) / 2
  15. let y = (layer.bounds.size.height - size.height) / 2
  16. let radius = layer.bounds.size.width / 10
  17. let mask: MaskType = .gear(radius: Double(radius), cogs: 8)
  18. let circle = ActivityIndicatorShape.mask(type: mask).makeLayer(size: size, color: color)
  19. circle.frame = CGRect(x: x, y: y, width: size.width, height: size.height)
  20. circle.add(defaultAnimation, forKey: "animation")
  21. layer.addSublayer(circle)
  22. }
  23. }
  24. // MARK: - Setup
  25. private extension ActivityIndicatorAnimationGear {
  26. var defaultAnimation: CABasicAnimation {
  27. let rotationAnimation = CABasicAnimation(keyPath: .rotation)
  28. rotationAnimation.byValue = Float.pi * 2
  29. rotationAnimation.timingFunctionType = .linear
  30. rotationAnimation.duration = duration
  31. rotationAnimation.repeatCount = .infinity
  32. rotationAnimation.isRemovedOnCompletion = false
  33. return rotationAnimation
  34. }
  35. }